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
This is pretty straightforward, instead of HashMap<TypeId, T> you can use either HashMap<(TypeId, &str), T> or HashMap<TypeId, (T, &str)> where &str is type_name::<T>().
It requires extra pointer in data structure. It also requires type names (static strings) to exist somewhere. I'm not sure Rust compiler can optimize them away if &str is stored but never used.
Formatting of values
You can format any values T where T: Debug by having every set member to be dyn DebugAny ; trait DebugAny: Debug + Any instead of dyn Any.
In case T doesn't implement Debug, custom implementation like Any { .. } can be provided.
The problem is: we can't currently check if T implements Debug and run fallback code if it doesn't.
require all T to be Debug
maybe separate ErasedSetDebug structure, but number of combinations with Send/Sync variants will be sad
or use generic dyn U instead of dyn Any as an argument of hashmap, and offload that problem to user
ask Rust team very nicely to implement specialization
Better Debug formatting would be nice.
For the keys, I have implemented a prototype in e1289aa (disabled in release mode).
As for the values, I'm inclined to wait for #7, which would make something like this work out of the box:
Example:
Produces this:
I'd like output to be more like this:
Formatting of keys
This is pretty straightforward, instead of
HashMap<TypeId, T>
you can use eitherHashMap<(TypeId, &str), T>
orHashMap<TypeId, (T, &str)>
where&str
istype_name::<T>()
.One possible implementation is in this blog post:
https://lucumr.pocoo.org/2022/1/7/as-any-hack/
Downsides:
It requires extra pointer in data structure. It also requires type names (static strings) to exist somewhere. I'm not sure Rust compiler can optimize them away if &str is stored but never used.
Formatting of values
You can format any values
T
whereT: Debug
by having every set member to bedyn DebugAny ; trait DebugAny: Debug + Any
instead ofdyn Any
.In case
T
doesn't implementDebug
, custom implementation likeAny { .. }
can be provided.The problem is: we can't currently check if
T
implementsDebug
and run fallback code if it doesn't.T
to beDebug
dyn U
instead ofdyn Any
as an argument of hashmap, and offload that problem to userConclusions
Possible - yes. Worth it - don't know.
Am I worrying about debug info too much?
The text was updated successfully, but these errors were encountered: