structural/field/rev_get_field/
components.rs

1use crate::{
2    enums::{EnumExt, IsVariant, VariantProxy},
3    field::{
4        FailedAccess, FieldType, GetField, GetFieldMut, GetFieldType, GetVariantField,
5        GetVariantFieldMut, InfallibleAccess, IntoField, IntoVariantField, MovedOutFields,
6        RevFieldErr, RevFieldType, RevGetFieldImpl, RevGetFieldMutImpl, RevIntoFieldImpl,
7        RevMoveOutFieldImpl,
8    },
9    TStr, VariantField, VariantName,
10};
11
12////////////////////////////////////////////////////////////////////////////////////////
13////                    TStr
14////////////////////////////////////////////////////////////////////////////////////////
15
16// Used as an implementation detail of the specialized RevGetFieldMutImpl impls.
17//
18// This was created because the error messages with specialization enabled were worse,
19// it said `VariantField<_,_> does not implement RevGetFieldMutImpl<'_,Foo>`,
20// when it should have said `Foo does not implement GetFieldMut<VariantField<_,_>>`,
21// which it does with this.
22#[doc(hidden)]
23unsafe trait SpecRevGetFieldMut<'a, This: ?Sized>: RevGetFieldImpl<'a, This> {
24    unsafe fn rev_get_field_raw_mut_inner(
25        self,
26        this: *mut This,
27    ) -> Result<*mut Self::Ty, Self::Err>;
28}
29
30impl<This, T> RevFieldType<This> for TStr<T>
31where
32    This: ?Sized + FieldType<Self>,
33{
34    type Ty = GetFieldType<This, Self>;
35}
36
37impl<This, T> RevFieldErr<This> for TStr<T>
38where
39    This: ?Sized + FieldType<Self>,
40{
41    type Err = InfallibleAccess;
42}
43
44impl<'a, This, T> RevGetFieldImpl<'a, This> for TStr<T>
45where
46    This: ?Sized + 'a + GetField<Self>,
47    This::Ty: 'a,
48{
49    #[inline(always)]
50    fn rev_get_field(self, this: &'a This) -> Result<&'a This::Ty, InfallibleAccess> {
51        Ok(GetField::get_field_(this, self))
52    }
53}
54
55unsafe impl<'a, This, T> RevGetFieldMutImpl<'a, This> for TStr<T>
56where
57    This: ?Sized + 'a + GetFieldMut<Self>,
58    This::Ty: 'a,
59{
60    #[inline(always)]
61    fn rev_get_field_mut(self, this: &'a mut This) -> Result<&'a mut This::Ty, InfallibleAccess> {
62        Ok(GetFieldMut::get_field_mut_(this, self))
63    }
64
65    #[inline(always)]
66    unsafe fn rev_get_field_raw_mut(
67        self,
68        this: *mut This,
69    ) -> Result<*mut This::Ty, InfallibleAccess> {
70        SpecRevGetFieldMut::<'a, This>::rev_get_field_raw_mut_inner(self, this)
71    }
72}
73
74unsafe impl<'a, This, T> SpecRevGetFieldMut<'a, This> for TStr<T>
75where
76    This: ?Sized + 'a + GetFieldMut<Self>,
77    This::Ty: 'a,
78{
79    default_if! {
80        #[inline(always)]
81        cfg(feature="specialization")
82        unsafe fn rev_get_field_raw_mut_inner(
83            self,
84            this:*mut  This
85        )-> Result<*mut This::Ty,InfallibleAccess>{
86            let func=(*this).get_field_raw_mut_fn();
87            Ok(func(
88                this as *mut  (),
89                self,
90            ))
91        }
92    }
93}
94
95#[cfg(feature = "specialization")]
96unsafe impl<'a, This, T> SpecRevGetFieldMut<'a, This> for TStr<T>
97where
98    This: 'a + GetFieldMut<Self>,
99    This::Ty: 'a,
100{
101    #[inline(always)]
102    unsafe fn rev_get_field_raw_mut_inner(
103        self,
104        this: *mut This,
105    ) -> Result<*mut This::Ty, InfallibleAccess> {
106        Ok(<This as GetFieldMut<Self>>::get_field_raw_mut(
107            this as *mut (),
108            self,
109        ))
110    }
111}
112
113impl<This, T> RevIntoFieldImpl<This> for TStr<T>
114where
115    This: ?Sized + IntoField<Self>,
116{
117    #[inline(always)]
118    fn rev_into_field(self, this: This) -> Result<This::Ty, InfallibleAccess>
119    where
120        This: Sized,
121        Self::Ty: Sized,
122    {
123        Ok(this.into_field_(self))
124    }
125}
126
127unsafe impl<This, T> RevMoveOutFieldImpl<This> for TStr<T>
128where
129    This: ?Sized + IntoField<Self>,
130{
131    unsafe fn rev_move_out_field(
132        self,
133        this: &mut This,
134        moved: &mut MovedOutFields,
135    ) -> Result<Self::Ty, Self::Err>
136    where
137        Self::Ty: Sized,
138    {
139        Ok(this.move_out_field_(self, moved))
140    }
141}
142
143////////////////////////////////////////////////////////////////////////////////////////
144////                    VariantField
145////////////////////////////////////////////////////////////////////////////////////////
146
147impl<This, _V, _F> RevFieldType<This> for VariantField<_V, _F>
148where
149    This: ?Sized + FieldType<Self>,
150{
151    type Ty = GetFieldType<This, Self>;
152}
153
154impl<This, _V, _F> RevFieldErr<This> for VariantField<_V, _F>
155where
156    This: ?Sized + FieldType<Self>,
157{
158    type Err = FailedAccess;
159}
160
161impl<'a, This, _V, _F> RevGetFieldImpl<'a, This> for VariantField<_V, _F>
162where
163    This: ?Sized + 'a + GetVariantField<_V, _F>,
164    This::Ty: 'a,
165{
166    #[inline(always)]
167    fn rev_get_field(self, this: &'a This) -> Result<&'a This::Ty, FailedAccess> {
168        ok_or_of!(GetVariantField::get_vfield_(this, self.variant, self.field))
169    }
170}
171
172unsafe impl<'a, This, _V, _F> RevGetFieldMutImpl<'a, This> for VariantField<_V, _F>
173where
174    This: ?Sized + 'a + GetVariantFieldMut<_V, _F>,
175    This::Ty: 'a,
176{
177    #[inline(always)]
178    fn rev_get_field_mut(self, this: &'a mut This) -> Result<&'a mut This::Ty, FailedAccess> {
179        ok_or_of!(GetVariantFieldMut::get_vfield_mut_(
180            this,
181            self.variant,
182            self.field
183        ))
184    }
185
186    #[inline(always)]
187    unsafe fn rev_get_field_raw_mut(self, this: *mut This) -> Result<*mut This::Ty, FailedAccess> {
188        SpecRevGetFieldMut::<'a, This>::rev_get_field_raw_mut_inner(self, this)
189    }
190}
191
192unsafe impl<'a, This, _V, _F> SpecRevGetFieldMut<'a, This> for VariantField<_V, _F>
193where
194    This: ?Sized + 'a + GetVariantFieldMut<_V, _F>,
195    This::Ty: 'a,
196{
197    default_if! {
198        #[inline(always)]
199        cfg(feature="specialization")
200        unsafe fn rev_get_field_raw_mut_inner(
201            self,
202            this:*mut  This
203        )-> Result<*mut This::Ty,FailedAccess>{
204            let func=(*this).get_vfield_raw_mut_fn();
205            match func( this as *mut  (), self.variant, self.field ) {
206                Some(x) => Ok(x.as_ptr()),
207                None => Err(FailedAccess),
208            }
209        }
210    }
211}
212
213#[cfg(feature = "specialization")]
214unsafe impl<'a, This, _V, _F> SpecRevGetFieldMut<'a, This> for VariantField<_V, _F>
215where
216    This: 'a + GetVariantFieldMut<_V, _F>,
217    This::Ty: 'a,
218{
219    #[inline(always)]
220    unsafe fn rev_get_field_raw_mut_inner(
221        self,
222        this: *mut This,
223    ) -> Result<*mut This::Ty, FailedAccess> {
224        let ret = <This as GetVariantFieldMut<_V, _F>>::get_vfield_raw_mut_(
225            this as *mut (),
226            self.variant,
227            self.field,
228        );
229        match ret {
230            Some(x) => Ok(x.as_ptr()),
231            None => Err(FailedAccess),
232        }
233    }
234}
235
236impl<This, _V, _F> RevIntoFieldImpl<This> for VariantField<_V, _F>
237where
238    This: ?Sized + IntoVariantField<_V, _F>,
239{
240    #[inline(always)]
241    fn rev_into_field(self, this: This) -> Result<This::Ty, FailedAccess>
242    where
243        This: Sized,
244    {
245        ok_or_of!(this.into_vfield_(self.variant, self.field))
246    }
247}
248
249unsafe impl<This, _V, _F> RevMoveOutFieldImpl<This> for VariantField<_V, _F>
250where
251    This: ?Sized + IntoVariantField<_V, _F>,
252{
253    #[inline(always)]
254    unsafe fn rev_move_out_field(
255        self,
256        this: &mut This,
257        moved: &mut MovedOutFields,
258    ) -> Result<This::Ty, FailedAccess>
259    where
260        This::Ty: Sized,
261    {
262        ok_or_of!(this.move_out_vfield_(self.variant, self.field, moved))
263    }
264}
265
266////////////////////////////////////////////////////////////////////////////////////////
267////                    VariantName
268////////////////////////////////////////////////////////////////////////////////////////
269
270impl<This, S> RevFieldType<This> for VariantName<TStr<S>>
271where
272    This: ?Sized + IsVariant<TStr<S>>,
273    S: 'static,
274{
275    type Ty = VariantProxy<This, TStr<S>>;
276}
277
278impl<This, S> RevFieldErr<This> for VariantName<TStr<S>>
279where
280    This: ?Sized + IsVariant<TStr<S>>,
281    S: 'static,
282{
283    type Err = FailedAccess;
284}
285
286impl<'a, This, S> RevGetFieldImpl<'a, This> for VariantName<TStr<S>>
287where
288    This: ?Sized + 'a + IsVariant<TStr<S>>,
289    S: 'static,
290{
291    #[inline(always)]
292    fn rev_get_field(
293        self,
294        this: &'a This,
295    ) -> Result<&'a VariantProxy<This, TStr<S>>, FailedAccess> {
296        map_of!(this.as_variant(self.name))
297    }
298}
299
300unsafe impl<'a, This, S> RevGetFieldMutImpl<'a, This> for VariantName<TStr<S>>
301where
302    This: ?Sized + 'a + IsVariant<TStr<S>>,
303    S: 'static,
304{
305    #[inline(always)]
306    fn rev_get_field_mut(
307        self,
308        this: &'a mut This,
309    ) -> Result<&'a mut VariantProxy<This, TStr<S>>, FailedAccess> {
310        map_of!(this.as_mut_variant(self.name))
311    }
312
313    #[inline(always)]
314    unsafe fn rev_get_field_raw_mut(
315        self,
316        this: *mut This,
317    ) -> Result<*mut VariantProxy<This, TStr<S>>, FailedAccess> {
318        map_of!(EnumExt::as_raw_mut_variant(this, self.name))
319    }
320}
321
322impl<This, S> RevIntoFieldImpl<This> for VariantName<TStr<S>>
323where
324    This: ?Sized + IsVariant<TStr<S>>,
325    S: 'static,
326{
327    #[inline(always)]
328    fn rev_into_field(self, this: This) -> Result<VariantProxy<This, TStr<S>>, FailedAccess>
329    where
330        This: Sized,
331    {
332        map_of!(this.into_variant(self.name))
333    }
334}