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 these examples, `C<T>` is the container type, and `F` is the field type.// Supports immutable and mutable references.let _:&C<F> = project!(&c.f);let _:&mutC<F> = project!(&mut c.f);// Supports arbitrary expressions to generate the container reference.let ident = |x| x;let _:&C<F> = project!(&(ident(&c)).f);// Supports chained field accesses.let _:&C<G> = project!(&c.f.g);// Supports indexing operations for elements and slices (bounds-checked at runtime).let _:&C<H> = project!(&c.f.g[0]);let _:&C<[H]> = project!(&c.f.g[1..3]);
This API is clean, but not that discoverable if you're looking at the rustdoc for a particular container type. Ideally, projection would be accomplished with a method which would show up in the container's rustdoc. Here's an idea for how to support projection via a method that is still reasonably ergonomic.
First, introduce a Projection type which represents projection into a particular field. This type is constructible using a macro.
/// The projection of `T`'s field of type `F`.structProjection<T,F>{ ... }let _:Projection<T,F> = project!(f);
Then a particular container type can add a project method which takes a Projection:
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
The API proposed in #196 looks like this:
This API is clean, but not that discoverable if you're looking at the rustdoc for a particular container type. Ideally, projection would be accomplished with a method which would show up in the container's rustdoc. Here's an idea for how to support projection via a method that is still reasonably ergonomic.
First, introduce a
Projection
type which represents projection into a particular field. This type is constructible using a macro.Then a particular container type can add a
project
method which takes aProjection
:Using the examples from the top, we could modify them as:
Beta Was this translation helpful? Give feedback.
All reactions