Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- trait Field {
- type ValueType;
- }
- use std::marker::PhantomData as Marker;
- struct X<T>(Marker<T>);
- struct Y<T>(Marker<T>);
- struct Z<T>(Marker<T>);
- impl<T> Field for X<T> {
- type ValueType = T;
- }
- impl<T> Field for Y<T> {
- type ValueType = T;
- }
- impl<T> Field for Z<T> {
- type ValueType = T;
- }
- trait HasField<F: Field> {
- fn field(&self) -> &F::ValueType;
- fn field_mut(&mut self) -> &mut F::ValueType;
- }
- #[derive(Debug, Default)]
- struct Vec3<T>(T, T, T);
- impl<T> HasField<X<T>> for Vec3<T> {
- fn field(&self) -> &<X<T> as Field>::ValueType { &self.0 }
- fn field_mut(&mut self) -> &mut <X<T> as Field>::ValueType { &mut self.0 }
- }
- impl<T> HasField<Y<T>> for Vec3<T> {
- fn field(&self) -> &<Y<T> as Field>::ValueType { &self.1 }
- fn field_mut(&mut self) -> &mut <Y<T> as Field>::ValueType { &mut self.1 }
- }
- impl<T> HasField<Z<T>> for Vec3<T> {
- fn field(&self) -> &<Z<T> as Field>::ValueType { &self.2 }
- fn field_mut(&mut self) -> &mut <Z<T> as Field>::ValueType { &mut self.2 }
- }
- fn main() {
- let mut vec = Vec3::<u32>::default();
- *HasField::<X<_>>::field_mut(&mut vec) = 1;
- *HasField::<Y<_>>::field_mut(&mut vec) = 2;
- *HasField::<Z<_>>::field_mut(&mut vec) = 3;
- println!("{:?}", vec);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement