You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In some cases, it may be desirable for types implementing Valuable to record themselves as a pre-formatted human-readable text representation. Currently, this is only possible with the Value::String variant, which takes a &'a str.
For types that want to record themselves using a Display (or Debug) implementation, though, Value::String isn't really sufficient. Because it takes a borrowed string slice, those types would have to allocate ahead of time and store their own formatted representation, so that the borrowed string slice can be handed out in their Valuable impls. This is not ideal.
Therefore, we should probably add &dyn fmt::Display as a primitive Value variant.
The text was updated successfully, but these errors were encountered:
The workaround I use is to return a single-value tuple that is a String. This works well enough, but does add a layer of noise in the output I would prefer to avoid.
Here's an example:
structMyStruct;implValuableforMyStruct{fnas_value(&self) -> Value<'_>{// We don't store a String of the value, so can't return an &str// and use `Value<'a>::String(&'a str)`, encode as a tuple instead.Value::Tuplable(self)}fnvisit(&self,visit:&mutdyn valuable::Visit){let s:String = foo();let val = Value::String(s.as_str());
visit.visit_unnamed_fields(&[val]);}}implTuplableforMyStruct{fndefinition(&self) -> TupleDef{TupleDef::new_static(1)}}
In some cases, it may be desirable for types implementing
Valuable
to record themselves as a pre-formatted human-readable text representation. Currently, this is only possible with theValue::String
variant, which takes a&'a str
.For types that want to record themselves using a
Display
(orDebug
) implementation, though,Value::String
isn't really sufficient. Because it takes a borrowed string slice, those types would have to allocate ahead of time and store their own formatted representation, so that the borrowed string slice can be handed out in theirValuable
impls. This is not ideal.Therefore, we should probably add
&dyn fmt::Display
as a primitiveValue
variant.The text was updated successfully, but these errors were encountered: