devela/sys/mem/
pin.rs

1// devela::sys::mem::pin
2//
3//! Defines [`Pinned`].
4//
5
6use crate::Pin;
7
8/// A wrapper for structurally pinned data.
9///
10/// Up to 8 generics can be supplied for 8 structurally pinned fields.
11#[rustfmt::skip]
12pub struct Pinned<A, B = (), C = (), D = (), E = (), F = (), G = (), H = ()> {
13    a: A, b: B, c: C, d: D, e: E, f: F, g: G, h: H
14}
15
16#[rustfmt::skip]
17mod impls {
18    use super::{Pin, Pinned};
19
20    impl<A> Pinned<A> {
21        /// Create a new `Pinned` with a single field
22        pub fn from_a(a: A) -> Self {
23            Pinned { a, b: (), c: (), d: (), e: (), f: (), g: (), h: () }
24        }
25        /// Add field `B` to a `Pinned`
26        pub fn with_b<B>(self, b: B) -> Pinned<A, B> {
27            Pinned { a: self.a,
28                b, c: (), d: (), e: (), f: (), g: (), h: ()
29            }
30        }
31    }
32    impl<A, B> Pinned<A, B> {
33        /// Add field `C` to a `Pinned`
34        pub fn with_c<C>(self, c: C) -> Pinned<A, B, C> {
35            Pinned {
36                a: self.a, b: self.b,
37                c, d: (), e: (), f: (), g: (), h: ()
38            }
39        }
40    }
41    impl<A, B, C> Pinned<A, B, C> {
42        /// Add field `D` to a `Pinned`
43        pub fn with_d<D>(self, d: D) -> Pinned<A, B, C, D> {
44            Pinned {
45                a: self.a, b: self.b, c: self.c,
46                d, e: (), f: (), g: (), h: (),
47            }
48        }
49    }
50    impl<A, B, C, D> Pinned<A, B, C, D> {
51        /// Add field `E` to a `Pinned`
52        pub fn with_e<E>(self, e: E) -> Pinned<A, B, C, D, E> {
53            Pinned {
54                a: self.a, b: self.b, c: self.c, d: self.d,
55                e, f: (), g: (), h: ()
56            }
57        }
58    }
59    impl<A, B, C, D, E> Pinned<A, B, C, D, E> {
60        /// Add field `F` to a `Pinned`
61        pub fn with_f<F>(self, f: F) -> Pinned<A, B, C, D, E, F> {
62            Pinned {
63                a: self.a, b: self.b, c: self.c, d: self.d, e: self.e,
64                f, g: (), h: (),
65            }
66        }
67    }
68    impl<A, B, C, D, E, F> Pinned<A, B, C, D, E, F> {
69        /// Add field `G` to a `Pinned`
70        pub fn with_g<G>(self, g: G) -> Pinned<A, B, C, D, E, F, G> {
71            Pinned {
72                a: self.a, b: self.b, c: self.c, d: self.d, e: self.e, f: self.f,
73                g, h: (),
74            }
75        }
76    }
77    impl<A, B, C, D, E, F, G> Pinned<A, B, C, D, E, F, G> {
78        /// Add field `H` to a `Pinned`
79        pub fn with_h<H>(self, h: H) -> Pinned<A, B, C, D, E, F, G, H> {
80            Pinned {
81                a: self.a, b: self.b, c: self.c, d: self.d, e: self.e, f: self.f, g: self.g,
82                h,
83            }
84        }
85    }
86
87    // Getters
88    impl<A, B, C, D, E, F, G, H> Pinned<A, B, C, D, E, F, G, H> {
89        /// Get a `Pin<&mut A>`
90        pub fn a(self: Pin<&mut Self>) -> Pin<&mut A> {
91            // SAFETY: This is okay because `a` is pinned when `self` is.
92            unsafe { self.map_unchecked_mut(|this| &mut this.a) }
93        }
94        /// Get a `Pin<&mut B>`
95        pub fn b(self: Pin<&mut Self>) -> Pin<&mut B> {
96            // SAFETY: This is okay because `b` is pinned when `self` is.
97            unsafe { self.map_unchecked_mut(|this| &mut this.b) }
98        }
99        /// Get a `Pin<&mut C>`
100        pub fn c(self: Pin<&mut Self>) -> Pin<&mut C> {
101            // SAFETY: This is okay because `c` is pinned when `self` is.
102            unsafe { self.map_unchecked_mut(|this| &mut this.c) }
103        }
104        /// Get a `Pin<&mut D>`
105        pub fn d(self: Pin<&mut Self>) -> Pin<&mut D> {
106            // SAFETY: This is okay because `d` is pinned when `self` is.
107            unsafe { self.map_unchecked_mut(|this| &mut this.d) }
108        }
109        /// Get a `Pin<&mut E>`
110        pub fn e(self: Pin<&mut Self>) -> Pin<&mut E> {
111            // SAFETY: This is okay because `e` is pinned when `self` is.
112            unsafe { self.map_unchecked_mut(|this| &mut this.e) }
113        }
114        /// Get a `Pin<&mut F>`
115        pub fn f(self: Pin<&mut Self>) -> Pin<&mut F> {
116            // SAFETY: This is okay because `f` is pinned when `self` is.
117            unsafe { self.map_unchecked_mut(|this| &mut this.f) }
118        }
119        /// Get a `Pin<&mut G>`
120        pub fn g(self: Pin<&mut Self>) -> Pin<&mut G> {
121            // SAFETY: This is okay because `g` is pinned when `self` is.
122            unsafe { self.map_unchecked_mut(|this| &mut this.g) }
123        }
124        /// Get a `Pin<&mut H>`
125        pub fn h(self: Pin<&mut Self>) -> Pin<&mut H> {
126            // SAFETY: This is okay because `h` is pinned when `self` is.
127            unsafe { self.map_unchecked_mut(|this| &mut this.h) }
128        }
129    }
130}