11/// A pinned projection of a struct field.
22///
3+ /// # Safety
4+ ///
35/// To make using this macro safe, three things need to be ensured:
46/// - If the struct implements [`Drop`], the [`drop`] method is not allowed to
57/// move the value of the field.
68/// - If the struct wants to implement [`Unpin`], it has to do so conditionally:
79/// The struct can only implement [`Unpin`] if the field's type is [`Unpin`].
810/// - The struct must not be `#[repr(packed)]`.
911///
10- /// ```
12+ /// # Example
13+ ///
14+ /// ```rust
1115/// use pin_utils::unsafe_pinned;
1216/// use std::marker::Unpin;
1317/// use std::pin::Pin;
2731/// impl<T: Unpin> Unpin for Foo<T> {} // Conditional Unpin impl
2832/// ```
2933///
30- /// Note that borrowing the field multiple times requires using `.as_mut()` to
34+ /// Note: borrowing the field multiple times requires using `.as_mut()` to
3135/// avoid consuming the `Pin`.
3236///
3337/// [`Unpin`]: core::marker::Unpin
@@ -37,10 +41,10 @@ macro_rules! unsafe_pinned {
3741 ( $f: tt: $t: ty) => (
3842 #[ allow( unsafe_code) ]
3943 fn $f<' __a>(
40- self : $crate :: core_reexport :: pin:: Pin <& ' __a mut Self >
41- ) -> $crate :: core_reexport :: pin:: Pin <& ' __a mut $t> {
44+ self : :: core :: pin:: Pin <& ' __a mut Self >
45+ ) -> :: core :: pin:: Pin <& ' __a mut $t> {
4246 unsafe {
43- $crate :: core_reexport :: pin:: Pin :: map_unchecked_mut(
47+ :: core :: pin:: Pin :: map_unchecked_mut(
4448 self , |x| & mut x. $f
4549 )
4650 }
@@ -50,15 +54,16 @@ macro_rules! unsafe_pinned {
5054
5155/// An unpinned projection of a struct field.
5256///
57+ /// # Safety
58+ ///
5359/// This macro is unsafe because it creates a method that returns a normal
5460/// non-pin reference to the struct field. It is up to the programmer to ensure
5561/// that the contained value can be considered not pinned in the current
5662/// context.
5763///
58- /// Note that borrowing the field multiple times requires using `.as_mut()` to
59- /// avoid consuming the `Pin`.
64+ /// # Example
6065///
61- /// ```
66+ /// ```rust
6267/// use pin_utils::unsafe_unpinned;
6368/// use std::pin::Pin;
6469///
@@ -75,15 +80,20 @@ macro_rules! unsafe_pinned {
7580/// }
7681/// }
7782/// ```
83+ ///
84+ /// Note: borrowing the field multiple times requires using `.as_mut()` to
85+ /// avoid consuming the [`Pin`].
86+ ///
87+ /// [`Pin`]: core::pin::Pin
7888#[ macro_export]
7989macro_rules! unsafe_unpinned {
8090 ( $f: tt: $t: ty) => (
8191 #[ allow( unsafe_code) ]
8292 fn $f<' __a>(
83- self : $crate :: core_reexport :: pin:: Pin <& ' __a mut Self >
93+ self : :: core :: pin:: Pin <& ' __a mut Self >
8494 ) -> & ' __a mut $t {
8595 unsafe {
86- & mut $crate :: core_reexport :: pin:: Pin :: get_unchecked_mut( self ) . $f
96+ & mut :: core :: pin:: Pin :: get_unchecked_mut( self ) . $f
8797 }
8898 }
8999 )
0 commit comments