pub struct VariantProxy<T: ?Sized, V> { /* private fields */ }Expand description
Enum wrapper,for accessing the fields of a particular variant
(determined by the V type parameter).
The V type parameter is a TStr.
Example type: VariantProxy<Enum,TS!(Foo)>,Foo being the name of the variant.
§Construction
These are 3 ways to construct a VariantProxy:
-
Safe: Calling StructuralExt methods with
fp!(::VariantName)as an argument, which returns anOption<VariantProxy<Self,TS!(VariantName)>>(reference methods return a reference to a VariantProxy). -
Safe: Calling an EnumExt method with
fp!(VariantName)as an argument -
Unsafe: Calling a VariantProxy constructor function.
§Generic parameters
T is the enum this wraps.
V is the name of the wrapped variant (example type:TS!(Bar)).
§Example
This example constructs a VariantProxy safely.
use structural::{fp,ts,StructuralExt,Structural,TS};
use structural::enums::{EnumExt,VariantProxy};
with_bar(Foo::Bar(0,"hello"));
with_bar(Foo2::Bar(0,"hello",true));
/// Runs some assertions.
///
/// # Panics
///
/// This function panics if you pass an enum whose current variant is **not** `Bar`.
fn with_bar<T>(this: T)
where
// `Foo_ESI` was generated by the `Structural` derive,
// it aliases the accessor traits for `Foo`,
// and also requires an enum with the same amount of variants as `Foo`
//
// `Debug` is required to print the enum in the `.expect(...)` error.
T: Foo_ESI + Copy + std::fmt::Debug
{
let mut proxy: VariantProxy<T, TS!(Bar)>=
this.into_variant(ts!(Bar))
.expect("Expected the `Bar` variant to be passed in");
assert_eq!( proxy.field_(fp!(0)), &0);
assert_eq!( proxy.field_(fp!(1)), &"hello");
assert_eq!( proxy.field_mut(fp!(0)), &mut 0);
assert_eq!( proxy.field_mut(fp!(1)), &mut "hello");
assert_eq!( proxy.into_field(fp!(0)), 0);
assert_eq!( proxy.into_field(fp!(1)), "hello");
}
#[derive(Debug,PartialEq,Structural,Copy,Clone)]
enum Foo{
Bar(u32,&'static str),
Baz(u32),
}
#[derive(Debug,PartialEq,Structural,Copy,Clone)]
enum Foo2{
Bar(u32,&'static str,bool),
Baz(u32,u64),
}
§Example
This example uses an unsafe constructor.
use structural::{fp,ts,TS,StructuralExt,Structural};
use structural::enums::VariantProxy;
unsafe{
with_bar(Foo::Bar(0,"hello"));
with_bar(Foo2::Bar(0,"hello",true));
}
/// Runs some assertions.
///
/// # Safety
///
/// You must pass an enum whose current variant is `Bar`,
/// `this.is_variant(ts!(Bar))` (the method is from `StructuralExt`) must return true.
unsafe fn with_bar<T>(this: T)
where
// `Foo_ESI` was generated by the `Structural` derive,
// it aliases the accessor traits for `Foo`,
// and also requires an enum with the same amount of variants as `Foo`
T: Foo_ESI + Copy
{
let mut proxy: VariantProxy<T, TS!(Bar)>=
VariantProxy::new(this, ts!(Bar));
assert_eq!( proxy.field_(fp!(0)), &0);
assert_eq!( proxy.field_(fp!(1)), &"hello");
assert_eq!( proxy.field_mut(fp!(0)), &mut 0);
assert_eq!( proxy.field_mut(fp!(1)), &mut "hello");
assert_eq!( proxy.into_field(fp!(0)), 0);
assert_eq!( proxy.into_field(fp!(1)), "hello");
}
#[derive(Debug,PartialEq,Structural,Copy,Clone)]
enum Foo{
Bar(u32,&'static str),
Baz(u32),
}
#[derive(Debug,PartialEq,Structural,Copy,Clone)]
enum Foo2{
Bar(u32,&'static str,bool),
Baz(u32,u64),
}
§Example
This example demonstrates how you can use a VariantProxy to treat a
newtype variant as the struct that it wraps.
use structural::{StructuralExt, Structural, TS, fp};
use structural::enums::VariantProxy;
// `Point_SI` was generated by the `Structural` derive macro on `Point`,
// aliasing the accessor traits that `Point` implements.
fn with_bar(this: &impl Point_SI){
assert_eq!( this.fields(fp!(x,y)), (&21, &34) );
}
let point=Point{x:21, y:34};
with_bar(&point);
let variant=Foo::Point(point);
// The type annotation here isn't necessary.
let proxy: &VariantProxy<Foo, TS!(Point)>=
variant.field_(fp!(::Point)).expect("it was just constructed as a Foo::Point");
with_bar(proxy);
#[derive(Structural)]
// The `#[struc(no_trait)]` attribute disables the generation of
// the `*_SI` and `*_ESI` traits for this type.
#[struc(no_trait)]
pub enum Foo{
// You would write this as `#[struc(newtype(bounds = "Point_VSI<@variant>"))]`
// if you didn't use the `#[struc(no_trait)]` attribute.
#[struc(newtype)]
Point(Point),
}
#[derive(Structural)]
pub struct Point{
pub x:u32,
pub y:u32,
}Implementations§
Source§impl<T: ?Sized, V> VariantProxy<T, TStr<V>>
impl<T: ?Sized, V> VariantProxy<T, TStr<V>>
Sourcepub const unsafe fn new(value: T, name: TStr<V>) -> Selfwhere
T: Sized,
pub const unsafe fn new(value: T, name: TStr<V>) -> Selfwhere
T: Sized,
Constructs this VariantProxy from an enum.
§Safety
V must be the name of the wrapped enum variant.
§Example
use structural::{fp,ts,TS,field_path_aliases,StructuralExt,Structural};
use structural::enums::VariantProxy;
#[derive(Debug,PartialEq,Structural)]
#[struc(no_trait)]
enum Foo{
Bar(u32),
Baz(u64),
}
let proxy: VariantProxy<Foo, TS!(Bar)>= unsafe{
VariantProxy::new(Foo::Bar(0), ts!(Bar))
};
assert_eq!( proxy.into_field(fp!(0)), 0 );
Sourcepub unsafe fn from_ref(reference: &T, _: TStr<V>) -> &Self
pub unsafe fn from_ref(reference: &T, _: TStr<V>) -> &Self
Constructs this VariantProxy from a reference to an enum.
§Safety
V must be the name of the wrapped enum variant.
§Example
use structural::{fp,ts,TS,StructuralExt,Structural};
use structural::enums::VariantProxy;
#[derive(Debug,PartialEq,Structural)]
#[struc(no_trait)]
enum Foo{
Bar(u32,bool),
Baz(u32),
}
let proxy: &VariantProxy<Foo, TS!(Bar)>= unsafe{
VariantProxy::from_ref(&Foo::Bar(99,false), ts!(Bar))
};
assert_eq!( proxy.field_(fp!(0)), &99 );
assert_eq!( proxy.field_(fp!(1)), &false );
Sourcepub unsafe fn from_mut(reference: &mut T, vari: TStr<V>) -> &mut Self
pub unsafe fn from_mut(reference: &mut T, vari: TStr<V>) -> &mut Self
Constructs this VariantProxy from a mutable reference to the enum.
§Safety
V must be the name of the wrapped enum variant.
§Safety
V must be the name of the wrapped enum variant.
§Example
use structural::{fp,ts,TS,StructuralExt,Structural};
use structural::enums::VariantProxy;
#[derive(Debug,PartialEq,Structural)]
#[struc(no_trait)]
enum Foo{
Bar(u32),
Baz(Option<usize>,Vec<String>),
}
let mut this=Foo::Baz(Some(1),vec![]);
let proxy: &mut VariantProxy<Foo, TS!(Baz)>= unsafe{
VariantProxy::from_mut(&mut this, ts!(Baz))
};
assert_eq!( proxy.field_mut(fp!(0)), &mut Some(1) );
assert_eq!( proxy.field_mut(fp!(1)), &mut Vec::<String>::new() );
Sourcepub const unsafe fn from_raw_mut(ptr: *mut T, name: TStr<V>) -> *mut Self
pub const unsafe fn from_raw_mut(ptr: *mut T, name: TStr<V>) -> *mut Self
Constructs this VariantProxy from a raw pointer to the enum.
§Safety
V must be the name of the wrapped enum variant.
§Safety
V must be the name of the wrapped enum variant.
§Example
use structural::{fp,ts,TS,StructuralExt,Structural};
use structural::enums::VariantProxy;
use std::cmp::Ordering;
#[derive(Debug,PartialEq,Structural)]
#[struc(no_trait)]
enum Foo{
Bar(u32),
Baz(Ordering,&'static [u8]),
}
let mut this=Foo::Baz( Ordering::Less, &[0,1,2,3] );
let proxy: *mut VariantProxy<Foo, TS!(Baz)>= unsafe{
VariantProxy::from_raw_mut(&mut this as *mut Foo, ts!(Baz))
};
unsafe{
assert_eq!( (*proxy).field_mut(fp!(0)), &mut Ordering::Less );
assert_eq!( (*proxy).field_mut(fp!(1)), &mut &[0,1,2,3] );
}
Sourcepub fn get(&self) -> &T
pub fn get(&self) -> &T
Gets a reference to the wrapped enum.
§Example
use structural::{fp,ts,TS,StructuralExt,Structural};
use structural::enums::VariantProxy;
#[derive(Debug,PartialEq,Structural)]
#[struc(no_trait)]
enum Foo{
Bar(u32),
Baz(u32),
}
let proxy: VariantProxy<Foo, TS!(Bar)>= unsafe{
VariantProxy::new(Foo::Bar(0), ts!(Bar))
};
assert_eq!(proxy.get() , &Foo::Bar(0));
Sourcepub unsafe fn get_mut(&mut self) -> &mut T
pub unsafe fn get_mut(&mut self) -> &mut T
Gets a mutable reference to the wrapped enum.
§Safety
You must not change the variant of the wrapped enum,
since VariantProxy relies on it being the one that the V
generic parmeter specifies
§Example
use structural::{fp,ts,TS,StructuralExt,Structural};
use structural::enums::VariantProxy;
#[derive(Debug,PartialEq,Structural)]
#[struc(no_trait)]
enum Foo{
Bar(u32),
Baz(u32),
}
let mut this=Foo::Baz(0);
let proxy: &mut VariantProxy<Foo, TS!(Baz)>= unsafe{
VariantProxy::from_mut(&mut this, ts!(Baz))
};
assert_eq!(unsafe{ proxy.get_mut() }, &mut Foo::Baz(0));
Sourcepub fn into_inner(self) -> Twhere
T: Sized,
pub fn into_inner(self) -> Twhere
T: Sized,
Unwraps this VariantProxy into the enum it wraps.
§Example
use structural::{fp,ts,TS,StructuralExt,Structural};
use structural::enums::VariantProxy;
#[derive(Debug,PartialEq,Structural)]
#[struc(no_trait)]
enum Foo{
Bar(u32),
Baz(u32),
}
let proxy: VariantProxy<Foo, TS!(Bar)>= unsafe{
VariantProxy::new(Foo::Bar(0), ts!(Bar))
};
assert_eq!(proxy.into_inner() , Foo::Bar(0));
Sourcepub unsafe fn as_raw_mut(this: *mut Self) -> *mut T
pub unsafe fn as_raw_mut(this: *mut Self) -> *mut T
Gets a mutable raw pointer to the wrapped enum.
§Safety
You must not change the variant of the wrapped enum,
because VariantProxy relies on it being the one that the V
generic parmaetere specifies
§Example
use structural::{fp,ts,TS,StructuralExt,Structural};
use structural::enums::VariantProxy;
#[derive(Debug,PartialEq,Structural)]
#[struc(no_trait)]
enum Foo{
Bar(u32),
Baz(u32),
}
let mut this=Foo::Baz(0);
let proxy: *mut VariantProxy<Foo, TS!(Baz)>= unsafe{
VariantProxy::from_raw_mut(&mut this as *mut Foo, ts!(Baz))
};
assert_eq!(unsafe{ &mut *VariantProxy::as_raw_mut(proxy) }, &mut Foo::Baz(0));
Trait Implementations§
Source§impl<T: Clone + ?Sized, V: Clone> Clone for VariantProxy<T, V>
impl<T: Clone + ?Sized, V: Clone> Clone for VariantProxy<T, V>
Source§fn clone(&self) -> VariantProxy<T, V>
fn clone(&self) -> VariantProxy<T, V>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<T, V> Debug for VariantProxy<T, V>
impl<T, V> Debug for VariantProxy<T, V>
Source§impl<T, V> DropFields for VariantProxy<T, V>
impl<T, V> DropFields for VariantProxy<T, V>
Source§unsafe fn drop_fields(&mut self, moved: MovedOutFields)
unsafe fn drop_fields(&mut self, moved: MovedOutFields)
Source§impl<T, V, F> FieldType<F> for VariantProxy<T, V>
impl<T, V, F> FieldType<F> for VariantProxy<T, V>
Source§impl<T, V, F> GetField<F> for VariantProxy<T, V>
impl<T, V, F> GetField<F> for VariantProxy<T, V>
Source§fn get_field_(&self, fname: F) -> &T::Ty
fn get_field_(&self, fname: F) -> &T::Ty
FieldName field by reference.Source§impl<T, V, F> GetFieldMut<F> for VariantProxy<T, V>
impl<T, V, F> GetFieldMut<F> for VariantProxy<T, V>
Source§fn get_field_mut_(&mut self, fname: F) -> &mut T::Ty
fn get_field_mut_(&mut self, fname: F) -> &mut T::Ty
FieldName field by mutable reference.Source§unsafe fn get_field_raw_mut(this: *mut (), fname: F) -> *mut T::Tywhere
Self: Sized,
unsafe fn get_field_raw_mut(this: *mut (), fname: F) -> *mut T::Tywhere
Self: Sized,
Source§fn get_field_raw_mut_fn(&self) -> GetFieldRawMutFn<F, T::Ty>
fn get_field_raw_mut_fn(&self) -> GetFieldRawMutFn<F, T::Ty>
get_field_raw_mut associated function as a function pointer.Source§impl<T, V, F> IntoField<F> for VariantProxy<T, V>where
T: IntoVariantField<V, F>,
V: IsTStr,
impl<T, V, F> IntoField<F> for VariantProxy<T, V>where
T: IntoVariantField<V, F>,
V: IsTStr,
Source§fn into_field_(self, fname: F) -> T::Tywhere
Self: Sized,
fn into_field_(self, fname: F) -> T::Tywhere
Self: Sized,
Source§unsafe fn move_out_field_(
&mut self,
fname: F,
moved: &mut MovedOutFields,
) -> T::Tywhere
Self: Sized,
unsafe fn move_out_field_(
&mut self,
fname: F,
moved: &mut MovedOutFields,
) -> T::Tywhere
Self: Sized,
Source§impl<T: Ord + ?Sized, V: Ord> Ord for VariantProxy<T, V>
impl<T: Ord + ?Sized, V: Ord> Ord for VariantProxy<T, V>
Source§fn cmp(&self, other: &VariantProxy<T, V>) -> Ordering
fn cmp(&self, other: &VariantProxy<T, V>) -> Ordering
1.21.0 · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl<T: PartialOrd + ?Sized, V: PartialOrd> PartialOrd for VariantProxy<T, V>
impl<T: PartialOrd + ?Sized, V: PartialOrd> PartialOrd for VariantProxy<T, V>
impl<T: Copy + ?Sized, V: Copy> Copy for VariantProxy<T, V>
impl<T: Eq + ?Sized, V: Eq> Eq for VariantProxy<T, V>
impl<T: ?Sized, V> StructuralPartialEq for VariantProxy<T, V>
Auto Trait Implementations§
impl<T, V> Freeze for VariantProxy<T, V>
impl<T, V> RefUnwindSafe for VariantProxy<T, V>
impl<T, V> Send for VariantProxy<T, V>
impl<T, V> Sync for VariantProxy<T, V>
impl<T, V> Unpin for VariantProxy<T, V>
impl<T, V> UnwindSafe for VariantProxy<T, V>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<This> EnumExt for Thiswhere
This: ?Sized,
impl<This> EnumExt for Thiswhere
This: ?Sized,
Source§fn as_variant<V>(
&self,
vari: TStr<V>,
) -> Result<&VariantProxy<Self, TStr<V>>, &Self>
fn as_variant<V>( &self, vari: TStr<V>, ) -> Result<&VariantProxy<Self, TStr<V>>, &Self>
Source§fn as_mut_variant<V>(
&mut self,
vari: TStr<V>,
) -> Result<&mut VariantProxy<Self, TStr<V>>, &mut Self>
fn as_mut_variant<V>( &mut self, vari: TStr<V>, ) -> Result<&mut VariantProxy<Self, TStr<V>>, &mut Self>
Source§impl<This, T> IntoStructural<T> for Thiswhere
T: FromStructural<This>,
impl<This, T> IntoStructural<T> for Thiswhere
T: FromStructural<This>,
Source§fn into_structural(self) -> T
fn into_structural(self) -> T
Source§impl<T> SelfOps for Twhere
T: ?Sized,
impl<T> SelfOps for Twhere
T: ?Sized,
Source§const T: PhantomData<fn() -> Self> = PhantomData
const T: PhantomData<fn() -> Self> = PhantomData
Type::T to pass it in methods with _:VariantPhantom<T> parameters. Read moreSource§const T_D: PhantomData<Self> = PhantomData
const T_D: PhantomData<Self> = PhantomData
Source§fn assert_ty(self, _other: PhantomData<fn() -> Self>) -> Selfwhere
Self: Sized,
fn assert_ty(self, _other: PhantomData<fn() -> Self>) -> Selfwhere
Self: Sized,
other is the same type as self.Source§fn assert_ty_ref(&self, _other: PhantomData<fn() -> Self>) -> &Selfwhere
Self: Sized,
fn assert_ty_ref(&self, _other: PhantomData<fn() -> Self>) -> &Selfwhere
Self: Sized,
other is the same type as self.Source§fn assert_ty_mut(&mut self, _other: PhantomData<fn() -> Self>) -> &mut Selfwhere
Self: Sized,
fn assert_ty_mut(&mut self, _other: PhantomData<fn() -> Self>) -> &mut Selfwhere
Self: Sized,
other is the same type as self.Source§fn ty_(&self) -> PhantomData<fn() -> Self>
fn ty_(&self) -> PhantomData<fn() -> Self>
Source§fn ty_inv(&self) -> PhantomData<fn(Self) -> Self>
fn ty_inv(&self) -> PhantomData<fn(Self) -> Self>
Source§fn ty_inv_ref(&self) -> PhantomData<Cell<&Self>>
fn ty_inv_ref(&self) -> PhantomData<Cell<&Self>>
Source§fn eq_id(&self, other: &Self) -> bool
fn eq_id(&self, other: &Self) -> bool
Source§fn piped<F, U>(self, f: F) -> U
fn piped<F, U>(self, f: F) -> U
Source§fn piped_ref<'a, F, U>(&'a self, f: F) -> Uwhere
F: FnOnce(&'a Self) -> U,
fn piped_ref<'a, F, U>(&'a self, f: F) -> Uwhere
F: FnOnce(&'a Self) -> U,
piped except that the function takes &Self
Useful for functions that take &Self instead of Self. Read moreSource§fn piped_mut<'a, F, U>(&'a mut self, f: F) -> Uwhere
F: FnOnce(&'a mut Self) -> U,
fn piped_mut<'a, F, U>(&'a mut self, f: F) -> Uwhere
F: FnOnce(&'a mut Self) -> U,
piped except that the function takes &mut Self.
Useful for functions that take &mut Self instead of Self.Source§fn mutated<F>(self, f: F) -> Self
fn mutated<F>(self, f: F) -> Self
Source§fn observe<F>(self, f: F) -> Self
fn observe<F>(self, f: F) -> Self
Source§fn into_<T>(self, _: PhantomData<fn() -> T>) -> Twhere
Self: Into<T>,
fn into_<T>(self, _: PhantomData<fn() -> T>) -> Twhere
Self: Into<T>,
Source§fn as_ref_<T>(&self) -> &T
fn as_ref_<T>(&self) -> &T
.as_ref_::<_>() syntax. Read moreSource§impl<T> StructuralExt for Twhere
T: ?Sized,
impl<T> StructuralExt for Twhere
T: ?Sized,
Source§fn field_<'a, P>(
&'a self,
path: P,
) -> NormalizeFieldsOut<Result<&'a P::Ty, P::Err>>
fn field_<'a, P>( &'a self, path: P, ) -> NormalizeFieldsOut<Result<&'a P::Ty, P::Err>>
path. Read moreSource§fn fields<'a, P>(&'a self, path: P) -> RevGetMultiFieldOut<'a, P, Self>where
P: RevGetMultiField<'a, Self>,
fn fields<'a, P>(&'a self, path: P) -> RevGetMultiFieldOut<'a, P, Self>where
P: RevGetMultiField<'a, Self>,
path. Read moreSource§fn cloned_fields<'a, P>(
&'a self,
path: P,
) -> ClonedOut<RevGetMultiFieldOut<'a, P, Self>>
fn cloned_fields<'a, P>( &'a self, path: P, ) -> ClonedOut<RevGetMultiFieldOut<'a, P, Self>>
path. Read moreSource§fn field_mut<'a, P>(
&'a mut self,
path: P,
) -> NormalizeFieldsOut<Result<&'a mut P::Ty, P::Err>>
fn field_mut<'a, P>( &'a mut self, path: P, ) -> NormalizeFieldsOut<Result<&'a mut P::Ty, P::Err>>
path. Read moreSource§fn fields_mut<'a, P>(
&'a mut self,
path: P,
) -> RevGetMultiFieldMutOut<'a, P, Self>where
P: RevGetMultiFieldMut<'a, Self>,
fn fields_mut<'a, P>(
&'a mut self,
path: P,
) -> RevGetMultiFieldMutOut<'a, P, Self>where
P: RevGetMultiFieldMut<'a, Self>,
path. Read moreSource§fn into_field<P>(self, path: P) -> NormalizeFieldsOut<Result<P::Ty, P::Err>>
fn into_field<P>(self, path: P) -> NormalizeFieldsOut<Result<P::Ty, P::Err>>
path. Read moreSource§fn into_fields<P>(self, path: P) -> RevIntoMultiFieldOut<P, Self>where
P: RevIntoMultiField<Self>,
Self: Sized,
fn into_fields<P>(self, path: P) -> RevIntoMultiFieldOut<P, Self>where
P: RevIntoMultiField<Self>,
Self: Sized,
self into multiple fields by value. Read moreSource§fn is_variant<P>(&self, _path: P) -> bool
fn is_variant<P>(&self, _path: P) -> bool
Source§fn into_struc<U>(self) -> Uwhere
Self: IntoStructural<U>,
fn into_struc<U>(self) -> Uwhere
Self: IntoStructural<U>,
IntoStructural. Read moreSource§fn try_into_struc<U>(self) -> Result<U, TryFromError<Self, Self::Error>>where
Self: TryIntoStructural<U>,
fn try_into_struc<U>(self) -> Result<U, TryFromError<Self, Self::Error>>where
Self: TryIntoStructural<U>,
TryIntoStructural. Read moreSource§impl<This, T> TryIntoStructural<T> for Thiswhere
T: TryFromStructural<This>,
impl<This, T> TryIntoStructural<T> for Thiswhere
T: TryFromStructural<This>,
Source§type Error = <T as TryFromStructural<This>>::Error
type Error = <T as TryFromStructural<This>>::Error
TryFromError,
returned from try_into_structural on conversion error.