Replies: 1 comment
-
I started to play with this idea a bit more, implementing a first prototype here. Besides the derive macro & primitives, i've implemented serde's #[derive(Introspect)]
struct User {
name: String,
email: String,
}
let user = /* .. */;
let json: String = serde_json::to_string(&Reflect(&user)).unwrap();
let user: User = serde_json::from_str::<Reflect<_>>(&json).0; |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I believe that a crucial missing feature of this library is the ability to stay within the category of types while doing reflection.
With
facet
as it is, I am forced to work withShape
, which only describes a type, with no good way to get back to the original type.I feel like this creates a big rift between the actual types of the type system and the description of types
Shape
provides 1.As a start, I'd love to see some way from a
Shape
back to the actual typeT
.For example, i'd love to be able to write the following:
(I'd be happy to also share some real use-cases other than this contrived example)
When I have implemented this "get all fields of a struct" operation in the past, I ended up with something similar to the following (playground):
That's certainly not very ergonomic, but it works.
An other approach I explored was to church-encode an actual list of types in rust, and treat that like any other type:
The final issue, however, is that just getting back the type
T
is not particularly useful, since the visitor in the above snippets can only call functions without trait bounds.I always solved this by introducing the trait bound I needed for my use-case, but that won't work in a general-purpose library.
I suppose some way to downcast from a concrete "T" to a "impl SomeTrait" would solve this problem - or maybe GATs could help? Not sure.
Footnotes
Slightly off-topic, but that's an issue I see with the serialization stuff in
facet
- it kind of bypasses the type system. Types no longer fully control how they are serialized, butfacet
does. ↩Beta Was this translation helpful? Give feedback.
All reactions