1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
//! An attribute that would create a projection struct covering all the fields. //! //! ## Examples //! //! ```rust //! use pin_project::unsafe_project; //! use std::pin::Pin; //! //! #[unsafe_project(Unpin)] //! struct Foo<T, U> { //! #[pin] //! future: T, //! field: U, //! } //! //! impl<T, U> Foo<T, U> { //! fn baz(mut self: Pin<&mut Self>) { //! let this = self.project(); //! let _: Pin<&mut T> = this.future; // Pinned reference to the field //! let _: &mut U = this.field; // Normal reference to the field //! } //! } //! //! // Automatically create the appropriate conditional Unpin implementation. //! // impl<T: Unpin, U> Unpin for Foo<T, U> {} // Conditional Unpin impl //! ``` //! //! See [`unsafe_project`] for more details. //! //! [`unsafe_project`]: ./attr.unsafe_project.html //! //! ## Rust Version //! //! The current version of pin-project requires Rust nightly 2018-12-26 or later. //! #![crate_type = "proc-macro"] #![recursion_limit = "256"] #![doc(html_root_url = "https://docs.rs/pin-project/0.1.5")] extern crate proc_macro; mod fields; mod variants {} mod enums {} mod structs; mod utils; mod compile_fail; use proc_macro::TokenStream; /// An attribute that would create a projection struct covering all the fields. /// /// This attribute creates a projection struct according to the following rules: /// /// - For the field that uses `#[pin]` attribute, makes the pinned reference to /// the field. /// - For the other fields, makes the unpinned reference to the field. /// /// ## Safety /// /// For the field that uses `#[pin]` attribute, three things need to be ensured: /// /// - If the struct implements [`Drop`], the [`drop`] method is not allowed to /// move the value of the field. /// - If the struct wants to implement [`Unpin`], it has to do so conditionally: /// The struct can only implement [`Unpin`] if the field's type is [`Unpin`]. /// If you use `#[unsafe_project(Unpin)]`, you do not need to ensure this /// because an appropriate conditional [`Unpin`] implementation will be /// generated. /// - The struct must not be `#[repr(packed)]`. /// /// For the other fields, need to be ensured that the contained value not pinned /// in the current context. /// /// ## Examples /// /// Using `#[unsafe_project(Unpin)]` will automatically create the appropriate /// conditional [`Unpin`] implementation: /// /// ```rust /// use pin_project::unsafe_project; /// use std::pin::Pin; /// /// #[unsafe_project(Unpin)] /// struct Foo<T, U> { /// #[pin] /// future: T, /// field: U, /// } /// /// impl<T, U> Foo<T, U> { /// fn baz(mut self: Pin<&mut Self>) { /// let this = self.project(); /// let _: Pin<&mut T> = this.future; // Pinned reference to the field /// let _: &mut U = this.field; // Normal reference to the field /// } /// } /// /// // Automatically create the appropriate conditional Unpin implementation. /// // impl<T, U> Unpin for Foo<T, U> where T: Unpin {} // Conditional Unpin impl /// ``` /// /// If you want to implement [`Unpin`] manually: /// /// ```rust /// use pin_project::unsafe_project; /// use std::marker::Unpin; /// use std::pin::Pin; /// /// #[unsafe_project] /// struct Foo<T, U> { /// #[pin] /// future: T, /// field: U, /// } /// /// impl<T, U> Foo<T, U> { /// fn baz(mut self: Pin<&mut Self>) { /// let this = self.project(); /// let _: Pin<&mut T> = this.future; // Pinned reference to the field /// let _: &mut U = this.field; // Normal reference to the field /// } /// } /// /// impl<T: Unpin, U> Unpin for Foo<T, U> {} // Conditional Unpin impl /// ``` /// /// Note that borrowing the field where `#[pin]` attribute is used multiple /// times requires using `.as_mut()` to avoid consuming the `Pin`. /// /// ## Supported Items /// /// The current version of pin-project supports the following two types of /// items. /// /// ### Structs (structs with named fields): /// /// ```rust /// # use pin_project::unsafe_project; /// # use std::pin::Pin; /// #[unsafe_project(Unpin)] /// struct Foo<T, U> { /// #[pin] /// future: T, /// field: U, /// } /// /// impl<T, U> Foo<T, U> { /// fn baz(mut self: Pin<&mut Self>) { /// let this = self.project(); /// let _: Pin<&mut T> = this.future; // Pinned reference to the field /// let _: &mut U = this.field; // Normal reference to the field /// } /// } /// ``` /// /// ### Tuple structs (structs with unnamed fields): /// /// ```rust /// # use pin_project::unsafe_project; /// # use std::pin::Pin; /// #[unsafe_project(Unpin)] /// struct Foo<T, U>(#[pin] T, U); /// /// impl<T, U> Foo<T, U> { /// fn baz(mut self: Pin<&mut Self>) { /// let this = self.project(); /// let _: Pin<&mut T> = this.0; // Pinned reference to the field /// let _: &mut U = this.1; // Normal reference to the field /// } /// } /// ``` /// /// Structs without fields (unit-like struct and zero fields struct) are not /// supported. /// /// [`Unpin`]: core::marker::Unpin /// [`drop`]: Drop::drop #[proc_macro_attribute] pub fn unsafe_project(args: TokenStream, input: TokenStream) -> TokenStream { structs::unsafe_project(args, input) } /// An attribute that would create projections for each struct fields. /// /// This is similar to [`unsafe_project`], but it is compatible with /// [pin-utils]. /// /// This attribute creates methods according to the following rules: /// /// - For the field that uses `#[pin]` attribute, the method that makes the /// pinned reference to that field is created. This is the same as /// [`pin_utils::unsafe_pinned`]. /// - For the field that uses `#[skip]` attribute, the method referencing that /// field is not created. /// - For the other fields, the method that makes the unpinned reference to that /// field is created.This is the same as [`pin_utils::unsafe_unpinned`]. /// /// ## Safety /// /// For the field that uses `#[pin]` attribute, three things need to be ensured: /// /// - If the struct implements [`Drop`], the [`drop`] method is not allowed to /// move the value of the field. /// - If the struct wants to implement [`Unpin`], it has to do so conditionally: /// The struct can only implement [`Unpin`] if the field's type is [`Unpin`]. /// If you use `#[unsafe_fields(Unpin)]`, you do not need to ensure this /// because an appropriate [`Unpin`] implementation will be generated. /// - The struct must not be `#[repr(packed)]`. /// /// For the other fields, need to be ensured that the contained value not pinned /// in the current context. /// /// ## Examples /// /// Using `#[unsafe_fields(Unpin)]` will automatically create the appropriate /// [`Unpin`] implementation: /// /// ```rust /// use pin_project::unsafe_fields; /// use std::pin::Pin; /// /// #[unsafe_fields(Unpin)] /// struct Foo<T, U> { /// #[pin] /// future: T, /// field: U, /// } /// /// impl<T, U> Foo<T, U> { /// fn baz(mut self: Pin<&mut Self>) { /// let _: Pin<&mut T> = self.as_mut().future(); // Pinned reference to the field /// let _: &mut U = self.as_mut().field(); // Normal reference to the field /// } /// } /// /// // You do not need to implement this manually. /// // impl<T, U> Unpin for Foo<T, U> where T: Unpin {} // Conditional Unpin impl /// ``` /// /// If you want to implement [`Unpin`] manually: /// /// ```rust /// use pin_project::unsafe_fields; /// use std::marker::Unpin; /// use std::pin::Pin; /// /// #[unsafe_fields] /// struct Foo<T, U> { /// #[pin] /// future: T, /// field: U, /// } /// /// impl<T, U> Foo<T, U> { /// fn baz(mut self: Pin<&mut Self>) { /// let _: Pin<&mut T> = self.as_mut().future(); // Pinned reference to the field /// let _: &mut U = self.as_mut().field(); // Normal reference to the field /// } /// } /// /// impl<T: Unpin, U> Unpin for Foo<T, U> {} // Conditional Unpin impl /// ``` /// /// Note that borrowing the field multiple times requires using `.as_mut()` to /// avoid consuming the `Pin`. /// /// [`unsafe_project`]: ./attr.unsafe_project.html /// [`Unpin`]: core::marker::Unpin /// [`drop`]: Drop::drop /// [pin-utils]: https://github.com/rust-lang-nursery/pin-utils /// [`pin_utils::unsafe_pinned`]: https://docs.rs/pin-utils/0.1.0-alpha/pin_utils/macro.unsafe_pinned.html /// [`pin_utils::unsafe_unpinned`]: https://docs.rs/pin-utils/0.1.0-alpha/pin_utils/macro.unsafe_unpinned.html #[proc_macro_attribute] pub fn unsafe_fields(args: TokenStream, input: TokenStream) -> TokenStream { fields::unsafe_fields(args, input) }