Skip to main content

pin_utils/
projection.rs

1/// A pinned projection of a struct field.
2///
3/// # Safety
4///
5/// To make using this macro safe, three things need to be ensured:
6/// - If the struct implements [`Drop`], the [`drop`] method is not allowed to
7///   move the value of the field.
8/// - If the struct wants to implement [`Unpin`], it has to do so conditionally:
9///   The struct can only implement [`Unpin`] if the field's type is [`Unpin`].
10/// - The struct must not be `#[repr(packed)]`.
11///
12/// # Example
13///
14/// ```rust
15/// use pin_utils::unsafe_pinned;
16/// use std::marker::Unpin;
17/// use std::pin::Pin;
18///
19/// struct Foo<T> {
20///     field: T,
21/// }
22///
23/// impl<T> Foo<T> {
24///     unsafe_pinned!(field: T);
25///
26///     fn baz(mut self: Pin<&mut Self>) {
27///         let _: Pin<&mut T> = self.field(); // Pinned reference to the field
28///     }
29/// }
30///
31/// impl<T: Unpin> Unpin for Foo<T> {} // Conditional Unpin impl
32/// ```
33///
34/// Note: borrowing the field multiple times requires using `.as_mut()` to
35/// avoid consuming the `Pin`.
36///
37/// [`Unpin`]: core::marker::Unpin
38/// [`drop`]: Drop::drop
39#[deprecated(
40    since = "0.1.1",
41    note = "this macro is not safe; use safe pin-project or pin-project-lite crate instead"
42)]
43#[macro_export]
44macro_rules! unsafe_pinned {
45    ($f:tt: $t:ty) => {
46        #[allow(unsafe_code)]
47        fn $f<'__a>(
48            self: $crate::__private::Pin<&'__a mut Self>,
49        ) -> $crate::__private::Pin<&'__a mut $t> {
50            unsafe { $crate::__private::Pin::map_unchecked_mut(self, |x| &mut x.$f) }
51        }
52    };
53}
54
55/// An unpinned projection of a struct field.
56///
57/// # Safety
58///
59/// This macro is unsafe because it creates a method that returns a normal
60/// non-pin reference to the struct field. It is up to the programmer to ensure
61/// that the contained value can be considered not pinned in the current
62/// context.
63///
64/// # Example
65///
66/// ```rust
67/// use pin_utils::unsafe_unpinned;
68/// use std::pin::Pin;
69///
70/// struct Bar;
71/// struct Foo {
72///     field: Bar,
73/// }
74///
75/// impl Foo {
76///     unsafe_unpinned!(field: Bar);
77///
78///     fn baz(mut self: Pin<&mut Self>) {
79///         let _: &mut Bar = self.field(); // Normal reference to the field
80///     }
81/// }
82/// ```
83///
84/// Note: borrowing the field multiple times requires using `.as_mut()` to
85/// avoid consuming the [`Pin`].
86///
87/// [`Pin`]: core::pin::Pin
88#[deprecated(
89    since = "0.1.1",
90    note = "this macro is not safe; use safe pin-project or pin-project-lite crate instead"
91)]
92#[macro_export]
93macro_rules! unsafe_unpinned {
94    ($f:tt: $t:ty) => {
95        #[allow(unsafe_code)]
96        fn $f<'__a>(self: $crate::__private::Pin<&'__a mut Self>) -> &'__a mut $t {
97            unsafe { &mut $crate::__private::Pin::get_unchecked_mut(self).$f }
98        }
99    };
100}