Skip to main content

structural/field/
most_impls.rs

1#![allow(non_camel_case_types)]
2
3use crate::{fp, FieldType, GetField, IntoField, Structural};
4
5#[allow(unused_imports)]
6use crate::StructuralExt;
7
8use std_::{
9    //marker::Unpin,
10    mem::ManuallyDrop,
11    ops::Deref,
12    ops::{Range, RangeFrom, RangeInclusive, RangeTo, RangeToInclusive},
13    pin::Pin,
14};
15
16type Start_STR = FP!(start);
17type End_STR = FP!(end);
18
19///////////////////////////////////////////////////////
20
21_private_impl_getters_for_derive_struct! {
22    impl[T] Range<T>
23    where[]
24    {
25        DropFields{ drop_fields={just_fields,} }
26
27        (IntoFieldMut < start : T,0,Start_STR,"start",> )
28        (IntoFieldMut < end : T,1,End_STR,"end",> )
29    }
30}
31
32z_impl_from_structural! {
33    impl[F, T] FromStructural<F> for Range<T>
34    where[
35        F: IntoField<Start_STR, Ty = T> + IntoField<End_STR, Ty = T>,
36    ]{
37        fn from_structural(this){
38            let (start, end) = this.into_fields(fp!(start, end));
39            Self { start, end }
40        }
41    }
42}
43
44_private_impl_getters_for_derive_struct! {
45    impl[T] RangeFrom<T>
46    where[]
47    {
48        DropFields{ drop_fields={just_fields,} }
49
50        (IntoFieldMut < start : T,0,Start_STR,"start",> )
51    }
52}
53
54z_impl_from_structural! {
55    impl[F, T] FromStructural<F> for RangeFrom<T>
56    where[
57        F: IntoField<Start_STR, Ty = T>,
58    ]{
59        fn from_structural(this){
60            let start = this.into_field(fp!(start));
61            Self { start }
62        }
63    }
64}
65
66_private_impl_getters_for_derive_struct! {
67    impl[T] RangeTo<T>
68    where[]
69    {
70        DropFields{ drop_fields={just_fields,} }
71
72        (IntoFieldMut < end : T,0,End_STR,"end",> )
73    }
74}
75
76z_impl_from_structural! {
77    impl[F, T] FromStructural<F> for RangeTo<T>
78    where[
79        F: IntoField<End_STR, Ty = T>,
80    ]{
81        fn from_structural(this){
82            let end = this.into_field(fp!(end));
83            Self { end }
84        }
85    }
86}
87
88_private_impl_getters_for_derive_struct! {
89    impl[T] RangeToInclusive<T>
90    where[]
91    {
92        DropFields{ drop_fields={just_fields,} }
93
94        (IntoFieldMut < end : T,0,End_STR,"end",> )
95    }
96}
97
98z_impl_from_structural! {
99    impl[F, T] FromStructural<F> for RangeToInclusive<T>
100    where[
101        F: IntoField<End_STR, Ty = T>,
102    ]{
103        fn from_structural(this){
104            let end = this.into_field(fp!(end));
105            Self { end }
106        }
107    }
108}
109
110///////////////////////////////////////////////////////
111
112impl<T> Structural for RangeInclusive<T> {}
113
114impl<T> FieldType<Start_STR> for RangeInclusive<T> {
115    type Ty = T;
116}
117impl<T> FieldType<End_STR> for RangeInclusive<T> {
118    type Ty = T;
119}
120
121impl<T> GetField<Start_STR> for RangeInclusive<T> {
122    fn get_field_(&self, _: Start_STR) -> &Self::Ty {
123        self.start()
124    }
125}
126impl<T> GetField<End_STR> for RangeInclusive<T> {
127    fn get_field_(&self, _: End_STR) -> &Self::Ty {
128        self.end()
129    }
130}
131
132z_impl_from_structural! {
133    impl[F, T] FromStructural<F> for RangeInclusive<T>
134    where[
135        F: IntoField<Start_STR, Ty = T> + IntoField<End_STR, Ty = T>,
136    ]{
137        fn from_structural(this){
138            let (start, end) = this.into_fields(fp!(start, end));
139            Self::new(start, end)
140        }
141    }
142}
143
144///////////////////////////////////////////////////////
145
146// This allows using all the field accessors in T from `ManuallyDrop<T>`
147unsafe_delegate_structural_with! {
148    impl[T,] ManuallyDrop<T>
149    where[]
150    self_ident=this;
151    specialization_params(Sized);
152    delegating_to_type=T;
153
154    GetField { this }
155
156    GetFieldMut { this }
157    as_delegating_raw{ this as *mut ManuallyDrop<T> as *mut T }
158
159    FromStructural {
160        constructor = ManuallyDrop::new;
161    }
162}
163
164#[test]
165fn delegated_mdrop() {
166    let tup = (2, 3, 5, 8);
167    let mut mdrop = ManuallyDrop::new(tup);
168    assert_eq!(mdrop.fields(fp!(0, 1, 2, 3)), (&2, &3, &5, &8));
169
170    assert_eq!(
171        mdrop.fields_mut(fp!(0, 1, 2, 3)),
172        (&mut 2, &mut 3, &mut 5, &mut 8)
173    );
174}
175
176///////////////////////////////////////////////////////
177
178unsafe_delegate_structural_with! {
179    impl[P,] Pin<P>
180    where[
181        P:Deref,
182        P::Target:Sized,
183    ]
184    self_ident=this;
185    delegating_to_type=P::Target;
186
187    GetField { &*this }
188
189    FromStructural
190    where [ P::Target: Unpin, ]
191    {
192        converted_from = P;
193        constructor = Pin::new;
194    }
195}
196
197#[test]
198fn delegated_pin() {
199    let tup = (2, 3, 5, 8);
200    let pin = Pin::new(&tup);
201    assert_eq!(pin.fields(fp!(0, 1, 2, 3)), (&2, &3, &5, &8));
202    //assert_eq!( pin.fields_mut(fp!(0,1,2,3)), (&mut 2,&mut 3,&mut 5,&mut 8) );
203}
204
205///////////////////////////////////////////////////////
206
207#[cfg(feature = "alloc")]
208mod alloc_impls {
209    use crate::{
210        alloc::{boxed::Box, rc::Rc, sync::Arc},
211        field::{
212            ownership::{DropFields, IntoFieldsWrapper, MovedOutFields},
213            IntoField, IntoVariantField,
214        },
215        TStr,
216    };
217    use std_::mem::ManuallyDrop;
218
219    macro_rules! impl_shared_ptr_accessors {
220        ( $this:ident ) => {
221            unsafe_delegate_structural_with! {
222                impl[T,] $this<T>
223                where[T:?Sized,]
224
225                self_ident=this;
226                delegating_to_type=T;
227
228                GetField {
229                    &*this
230                }
231
232                FromStructural{
233                    constructor = $this::new;
234                }
235            }
236        };
237    }
238    impl_shared_ptr_accessors! {Arc}
239    impl_shared_ptr_accessors! {Rc}
240
241    unsafe_delegate_structural_with! {
242        impl[T,] Box<T>
243        where[T:?Sized,]
244
245        self_ident=this;
246        specialization_params(specialize_cfg(feature="specialization"));
247        delegating_to_type=T;
248
249        GetField {
250            &*this
251        }
252
253        GetFieldMut{
254            &mut **this
255        }
256        as_delegating_raw{
257            *(this as *mut Box<T> as *mut *mut T)
258        }
259
260        FromStructural {
261            constructor = Box::new;
262        }
263    }
264
265    unsafe impl<T, P> IntoField<P> for Box<T>
266    where
267        T: ?Sized + IntoField<P>,
268    {
269        #[inline(always)]
270        fn into_field_(self, path: P) -> Self::Ty {
271            unsafe {
272                let mut this = IntoFieldsWrapper::new(self);
273                let (this, moved) = this.inner_and_moved_mut();
274                let this: &mut T = this;
275                this.move_out_field_(path, moved)
276            }
277        }
278
279        #[inline(always)]
280        unsafe fn move_out_field_(&mut self, path: P, moved: &mut MovedOutFields) -> Self::Ty {
281            let this: &mut T = self;
282            this.move_out_field_(path, moved)
283        }
284    }
285
286    unsafe impl<T, V, F, Ty> IntoVariantField<TStr<V>, F> for Box<T>
287    where
288        T: ?Sized + IntoVariantField<TStr<V>, F, Ty = Ty>,
289    {
290        #[inline(always)]
291        fn into_vfield_(self, vname: TStr<V>, fname: F) -> Option<Ty> {
292            unsafe {
293                let mut this = IntoFieldsWrapper::new(self);
294                let (this, moved) = this.inner_and_moved_mut();
295                let this: &mut T = this;
296                this.move_out_vfield_(vname, fname, moved)
297            }
298        }
299
300        #[inline(always)]
301        unsafe fn move_out_vfield_(
302            &mut self,
303            vname: TStr<V>,
304            fname: F,
305            moved: &mut MovedOutFields,
306        ) -> Option<Ty> {
307            let this: &mut T = self;
308            this.move_out_vfield_(vname, fname, moved)
309        }
310    }
311
312    unsafe impl<T> DropFields for Box<T>
313    where
314        T: ?Sized + DropFields,
315    {
316        #[inline(always)]
317        fn pre_move(&mut self) {
318            <T as DropFields>::pre_move(&mut **self);
319        }
320
321        #[inline(always)]
322        unsafe fn drop_fields(&mut self, moved: MovedOutFields) {
323            let mut this = Box::<ManuallyDrop<T>>::from_raw(
324                &mut **({ self } as *mut Box<T> as *mut Box<ManuallyDrop<T>>),
325            );
326            let this: &mut T = &mut **this;
327            <T as DropFields>::drop_fields(this, moved)
328        }
329    }
330}
331
332///////////////////////////////////////////////////////
333
334unsafe_delegate_structural_with! {
335    impl['a,T,] &'a T
336    where [T:?Sized,]
337    self_ident=this;
338    delegating_to_type=T;
339
340    GetField { &**this }
341}
342
343///////////////////////////////////////////////////////
344
345unsafe_delegate_structural_with! {
346    impl['a,T:'a,] &'a mut T
347    where [T:?Sized,]
348    self_ident=this;
349    specialization_params(specialize_cfg(feature="specialization"));
350    delegating_to_type=T;
351
352    GetField { &**this }
353
354    GetFieldMut{
355        *this
356    }
357    as_delegating_raw{
358        *(this as *mut *mut T)
359    }
360}