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 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
//! A lightweight version of [pin-project] written with declarative macros. //! //! # Examples //! //! [`pin_project!`] macro creates a projection type covering all the fields of struct. //! //! ```rust //! use pin_project_lite::pin_project; //! use std::pin::Pin; //! //! pin_project! { //! struct Struct<T, U> { //! #[pin] //! pinned: T, //! unpinned: U, //! } //! } //! //! impl<T, U> Struct<T, U> { //! fn method(self: Pin<&mut Self>) { //! let this = self.project(); //! let _: Pin<&mut T> = this.pinned; // Pinned reference to the field //! let _: &mut U = this.unpinned; // Normal reference to the field //! } //! } //! ``` //! //! # [pin-project] vs pin-project-lite //! //! Here are some similarities and differences compared to [pin-project]. //! //! ## Similar: Safety //! //! pin-project-lite guarantees safety in much the same way as [pin-project]. Both are completely safe unless you write other unsafe code. //! //! ## Different: Minimal design //! //! This library does not tackle as expansive of a range of use cases as [pin-project] does. If your use case is not already covered, please use [pin-project]. //! //! ## Different: No proc-macro related dependencies //! //! This is the **only** reason to use this crate. However, **if you already have proc-macro related dependencies in your crate's dependency graph, there is no benefit from using this crate.** (Note: There is almost no difference in the amount of code generated between [pin-project] and pin-project-lite.) //! //! ## Different: No useful error messages //! //! This macro does not handle any invalid input. So error messages are not to be useful in most cases. If you do need useful error messages, then upon error you can pass the same input to [pin-project] to receive a helpful description of the compile error. //! //! ## Different: Structs only //! //! pin-project-lite will refuse anything other than a braced struct with named fields. Enums and tuple structs are not supported. //! //! ## Different: No support for custom Drop implementation //! //! pin-project supports this by [`#[pinned_drop]`][pinned-drop]. //! //! ## Different: No support for custom Unpin implementation //! //! pin-project supports this by [`UnsafeUnpin`][unsafe-unpin] and [`!Unpin`][not-unpin]. //! //! ## Different: No support for pattern matching and destructing //! //! [pin-project supports this.][naming] //! //! [naming]: https://docs.rs/pin-project/1/pin_project/attr.pin_project.html //! [not-unpin]: https://docs.rs/pin-project/1/pin_project/attr.pin_project.html#unpin //! [pin-project]: https://github.com/taiki-e/pin-project //! [pinned-drop]: https://docs.rs/pin-project/1/pin_project/attr.pin_project.html#pinned_drop //! [unsafe-unpin]: https://docs.rs/pin-project/1/pin_project/attr.pin_project.html#unsafeunpin #![no_std] #![doc(html_root_url = "https://docs.rs/pin-project-lite/0.1.11")] #![doc(test( no_crate_inject, attr(deny(warnings, rust_2018_idioms, single_use_lifetimes), allow(dead_code)) ))] #![warn(unsafe_code)] #![warn(future_incompatible, rust_2018_idioms, single_use_lifetimes, unreachable_pub)] #![warn(clippy::all, clippy::default_trait_access)] // mem::take and #[non_exhaustive] requires Rust 1.40, matches! requires Rust 1.42 #![allow( clippy::mem_replace_with_default, clippy::manual_non_exhaustive, clippy::match_like_matches_macro )] /// A macro that creates a projection type covering all the fields of struct. /// /// This macro creates a projection type 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. /// /// And the following methods are implemented on the original type: /// /// ```rust /// # use std::pin::Pin; /// # type Projection<'a> = &'a (); /// # type ProjectionRef<'a> = &'a (); /// # trait Dox { /// fn project(self: Pin<&mut Self>) -> Projection<'_>; /// fn project_ref(self: Pin<&Self>) -> ProjectionRef<'_>; /// # } /// ``` /// /// The visibility of the projected type and projection method is based on the /// original type. However, if the visibility of the original type is `pub`, /// the visibility of the projected type and the projection method is `pub(crate)`. /// /// # Safety /// /// `pin_project!` macro guarantees safety in much the same way as [pin-project] crate. /// Both are completely safe unless you write other unsafe code. /// /// See [pin-project] crate for more details. /// /// # Examples /// /// ```rust /// use pin_project_lite::pin_project; /// use std::pin::Pin; /// /// pin_project! { /// struct Struct<T, U> { /// #[pin] /// pinned: T, /// unpinned: U, /// } /// } /// /// impl<T, U> Struct<T, U> { /// fn method(self: Pin<&mut Self>) { /// let this = self.project(); /// let _: Pin<&mut T> = this.pinned; // Pinned reference to the field /// let _: &mut U = this.unpinned; // Normal reference to the field /// } /// } /// ``` /// /// If you want to call the `project()` method multiple times or later use the /// original [`Pin`] type, it needs to use [`.as_mut()`][`Pin::as_mut`] to avoid /// consuming the [`Pin`]. /// /// ```rust /// use pin_project_lite::pin_project; /// use std::pin::Pin; /// /// pin_project! { /// struct Struct<T> { /// #[pin] /// field: T, /// } /// } /// /// impl<T> Struct<T> { /// fn call_project_twice(mut self: Pin<&mut Self>) { /// // `project` consumes `self`, so reborrow the `Pin<&mut Self>` via `as_mut`. /// self.as_mut().project(); /// self.as_mut().project(); /// } /// } /// ``` /// /// # `!Unpin` /// /// If you want to ensure that [`Unpin`] is not implemented, use `#[pin]` /// attribute for a [`PhantomPinned`] field. /// /// ```rust /// use pin_project_lite::pin_project; /// use std::marker::PhantomPinned; /// /// pin_project! { /// struct Struct<T> { /// field: T, /// #[pin] // <------ This `#[pin]` is required to make `Struct` to `!Unpin`. /// _pin: PhantomPinned, /// } /// } /// ``` /// /// Note that using [`PhantomPinned`] without `#[pin]` attribute has no effect. /// /// [`PhantomPinned`]: core::marker::PhantomPinned /// [`Pin::as_mut`]: core::pin::Pin::as_mut /// [`Pin`]: core::pin::Pin /// [pin-project]: https://github.com/taiki-e/pin-project #[macro_export] macro_rules! pin_project { ($($tt:tt)*) => { $crate::__pin_project_internal! { $($tt)* } }; } // limitations: // * no support for tuple structs and enums. // * no support for naming the projection types. // * no support for multiple trait/lifetime bounds. // * no support for `Self` in where clauses. (wontfix) // * no support for overlapping lifetime names. (wontfix) // * no interoperability with other field attributes. // * no useful error messages. (wontfix) // etc... // Not public API. #[doc(hidden)] #[macro_export] macro_rules! __pin_project_internal { // ============================================================================================= // main: struct (@struct_internal; [$proj_vis:vis] [$(#[$attrs:meta])* $vis:vis struct $ident:ident] [$($def_generics:tt)*] [$($impl_generics:tt)*] [$($ty_generics:tt)*] [$(where $($where_clause:tt)*)?] { $( $(#[$pin:ident])? $field_vis:vis $field:ident: $field_ty:ty ),+ } ) => { $(#[$attrs])* $vis struct $ident $($def_generics)* $(where $($where_clause)*)? { $( $field_vis $field: $field_ty ),+ } #[allow(explicit_outlives_requirements)] #[allow(single_use_lifetimes)] // https://github.com/rust-lang/rust/issues/55058 #[allow(clippy::redundant_pub_crate)] #[allow(clippy::used_underscore_binding)] const _: () = { $crate::__pin_project_internal! { @make_proj_ty_struct; [$proj_vis] [$vis struct $ident] [$($impl_generics)*] [$($ty_generics)*] [$(where $($where_clause)*)?] { $( $(#[$pin])? $field_vis $field: $field_ty ),+ } } impl <$($impl_generics)*> $ident <$($ty_generics)*> $(where $($where_clause)*)? { $proj_vis fn project<'__pin>( self: $crate::__private::Pin<&'__pin mut Self>, ) -> Projection <'__pin, $($ty_generics)*> { unsafe { let Self { $($field),* } = self.get_unchecked_mut(); Projection { $( $field: $crate::__pin_project_internal!(@make_unsafe_field_proj; $(#[$pin])? $field ) ),+ } } } $proj_vis fn project_ref<'__pin>( self: $crate::__private::Pin<&'__pin Self>, ) -> ProjectionRef <'__pin, $($ty_generics)*> { unsafe { let Self { $($field),* } = self.get_ref(); ProjectionRef { $( $field: $crate::__pin_project_internal!(@make_unsafe_field_proj; $(#[$pin])? $field ) ),+ } } } } $crate::__pin_project_internal! { @make_unpin_impl; [$vis $ident] [$($impl_generics)*] [$($ty_generics)*] [$(where $($where_clause)*)?] $( $field: $crate::__pin_project_internal!(@make_unpin_bound; $(#[$pin])? $field_ty ) ),+ } $crate::__pin_project_internal! { @make_drop_impl; [$ident] [$($impl_generics)*] [$($ty_generics)*] [$(where $($where_clause)*)?] } // Ensure that it's impossible to use pin projections on a #[repr(packed)] struct. // // Taking a reference to a packed field is unsafe, amd appplying // #[forbid(safe_packed_borrows)] makes sure that doing this without // an 'unsafe' block (which we deliberately do not generate) // is a hard error. // // If the struct ends up having #[repr(packed)] applied somehow, // this will generate an (unfriendly) error message. Under all reasonable // circumstances, we'll detect the #[repr(packed)] attribute, and generate // a much nicer error above. // // See https://github.com/taiki-e/pin-project/pull/34 for more details. #[forbid(safe_packed_borrows)] fn __assert_not_repr_packed <$($impl_generics)*> (this: &$ident <$($ty_generics)*>) $(where $($where_clause)*)? { $( let _ = &this.$field; )+ } }; }; // ============================================================================================= // make_proj_ty: struct (@make_proj_ty_struct; [$proj_vis:vis] [$vis:vis struct $ident:ident] [$($impl_generics:tt)*] [$($ty_generics:tt)*] [$(where $($where_clause:tt)* )?] { $( $(#[$pin:ident])? $field_vis:vis $field:ident: $field_ty:ty ),+ } ) => { #[allow(dead_code)] // This lint warns unused fields/variants. #[allow(clippy::mut_mut)] // This lint warns `&mut &mut <ty>`. #[allow(clippy::type_repetition_in_bounds)] // https://github.com/rust-lang/rust-clippy/issues/4326 $proj_vis struct Projection <'__pin, $($impl_generics)*> where $ident <$($ty_generics)*>: '__pin $(, $($where_clause)*)? { $( $field_vis $field: $crate::__pin_project_internal!(@make_proj_field; $(#[$pin])? $field_ty; mut ) ),+ } #[allow(dead_code)] // This lint warns unused fields/variants. #[allow(clippy::type_repetition_in_bounds)] // https://github.com/rust-lang/rust-clippy/issues/4326 $proj_vis struct ProjectionRef <'__pin, $($impl_generics)*> where $ident <$($ty_generics)*>: '__pin $(, $($where_clause)*)? { $( $field_vis $field: $crate::__pin_project_internal!(@make_proj_field; $(#[$pin])? $field_ty; ) ),+ } }; // ============================================================================================= // make_unpin_impl (@make_unpin_impl; [$vis:vis $ident:ident] [$($impl_generics:tt)*] [$($ty_generics:tt)*] [$(where $($where_clause:tt)* )?] $($field:tt)* ) => { // Automatically create the appropriate conditional `Unpin` implementation. // // Basically this is equivalent to the following code: // ```rust // impl<T, U> Unpin for Struct<T, U> where T: Unpin {} // ``` // // However, if struct is public and there is a private type field, // this would cause an E0446 (private type in public interface). // // When RFC 2145 is implemented (rust-lang/rust#48054), // this will become a lint, rather then a hard error. // // As a workaround for this, we generate a new struct, containing all of the pinned // fields from our #[pin_project] type. This struct is delcared within // a function, which makes it impossible to be named by user code. // This guarnatees that it will use the default auto-trait impl for Unpin - // that is, it will implement Unpin iff all of its fields implement Unpin. // This type can be safely declared as 'public', satisfiying the privacy // checker without actually allowing user code to access it. // // This allows users to apply the #[pin_project] attribute to types // regardless of the privacy of the types of their fields. // // See also https://github.com/taiki-e/pin-project/pull/53. $vis struct __Origin <'__pin, $($impl_generics)*> $(where $($where_clause)*)? { __dummy_lifetime: $crate::__private::PhantomData<&'__pin ()>, $($field)* } impl <'__pin, $($impl_generics)*> $crate::__private::Unpin for $ident <$($ty_generics)*> where __Origin <'__pin, $($ty_generics)*>: $crate::__private::Unpin $(, $($where_clause)*)? { } }; // ============================================================================================= // make_drop_impl (@make_drop_impl; [$ident:ident] [$($impl_generics:tt)*] [$($ty_generics:tt)*] [$(where $($where_clause:tt)* )?] ) => { // Ensure that struct does not implement `Drop`. // // There are two possible cases: // 1. The user type does not implement Drop. In this case, // the first blanked impl will not apply to it. This code // will compile, as there is only one impl of MustNotImplDrop for the user type // 2. The user type does impl Drop. This will make the blanket impl applicable, // which will then comflict with the explicit MustNotImplDrop impl below. // This will result in a compilation error, which is exactly what we want. trait MustNotImplDrop {} #[allow(clippy::drop_bounds, drop_bounds)] impl<T: $crate::__private::Drop> MustNotImplDrop for T {} impl <$($impl_generics)*> MustNotImplDrop for $ident <$($ty_generics)*> $(where $($where_clause)*)? { } }; // ============================================================================================= // make_unpin_bound (@make_unpin_bound; #[pin] $field_ty:ty ) => { $field_ty }; (@make_unpin_bound; $field_ty:ty ) => { $crate::__private::AlwaysUnpin<$field_ty> }; // ============================================================================================= // make_unsafe_field_proj (@make_unsafe_field_proj; #[pin] $field:ident ) => { $crate::__private::Pin::new_unchecked($field) }; (@make_unsafe_field_proj; $field:ident ) => { $field }; // ============================================================================================= // make_proj_field (@make_proj_field; #[pin] $field_ty:ty; $($mut:ident)? ) => { $crate::__private::Pin<&'__pin $($mut)? ($field_ty)> }; (@make_proj_field; $field_ty:ty; $($mut:ident)? ) => { &'__pin $($mut)? ($field_ty) }; // ============================================================================================= // Parses input and determines visibility ( $(#[$attrs:meta])* pub struct $ident:ident $(< $( $lifetime:lifetime $(: $lifetime_bound:lifetime)? ),* $(,)? $( $generics:ident $(: $generics_bound:path)? $(: ?$generics_unsized_bound:path)? $(: $generics_lifetime_bound:lifetime)? $(= $generics_default:ty)? ),* $(,)? >)? $(where $( $where_clause_ty:ty $(: $where_clause_bound:path)? $(: ?$where_clause_unsized_bound:path)? $(: $where_clause_lifetime_bound:lifetime)? ),* $(,)? )? { $( $(#[$pin:ident])? $field_vis:vis $field:ident: $field_ty:ty ),+ $(,)? } ) => { $crate::__pin_project_internal! { @struct_internal; [pub(crate)] [$(#[$attrs])* pub struct $ident] [$(< $( $lifetime $(: $lifetime_bound)? ,)* $( $generics $(: $generics_bound)? $(: ?$generics_unsized_bound)? $(: $generics_lifetime_bound)? $(= $generics_default)? ),* >)?] [$( $( $lifetime $(: $lifetime_bound)? ,)* $( $generics $(: $generics_bound)? $(: ?$generics_unsized_bound)? $(: $generics_lifetime_bound)? ),* )?] [$( $( $lifetime ,)* $( $generics ),* )?] [$(where $( $where_clause_ty $(: $where_clause_bound)? $(: ?$where_clause_unsized_bound)? $(: $where_clause_lifetime_bound)? ),* )?] { $( $(#[$pin])? $field_vis $field: $field_ty ),+ } } }; ( $(#[$attrs:meta])* $vis:vis struct $ident:ident $(< $( $lifetime:lifetime $(: $lifetime_bound:lifetime)? ),* $(,)? $( $generics:ident $(: $generics_bound:path)? $(: ?$generics_unsized_bound:path)? $(: $generics_lifetime_bound:lifetime)? $(= $generics_default:ty)? ),* $(,)? >)? $(where $( $where_clause_ty:ty $(: $where_clause_bound:path)? $(: ?$where_clause_unsized_bound:path)? $(: $where_clause_lifetime_bound:lifetime)? ),* $(,)? )? { $( $(#[$pin:ident])? $field_vis:vis $field:ident: $field_ty:ty ),+ $(,)? } ) => { $crate::__pin_project_internal! { @struct_internal; [$vis] [$(#[$attrs])* $vis struct $ident] [$(< $( $lifetime $(: $lifetime_bound)? ,)* $( $generics $(: $generics_bound)? $(: ?$generics_unsized_bound)? $(: $generics_lifetime_bound)? $(= $generics_default)? ),* >)?] [$( $( $lifetime $(: $lifetime_bound)? ,)* $( $generics $(: $generics_bound)? $(: ?$generics_unsized_bound)? $(: $generics_lifetime_bound)? ),* )?] [$( $( $lifetime ,)* $( $generics ),* )?] [$(where $( $where_clause_ty $(: $where_clause_bound)? $(: ?$where_clause_unsized_bound)? $(: $where_clause_lifetime_bound)? ),* )?] { $( $(#[$pin])? $field_vis $field: $field_ty ),+ } } }; } // Not public API. #[doc(hidden)] pub mod __private { #[doc(hidden)] pub use core::{ marker::{PhantomData, Unpin}, ops::Drop, pin::Pin, }; // This is an internal helper struct used by `pin_project!`. #[doc(hidden)] pub struct AlwaysUnpin<T: ?Sized>(PhantomData<T>); impl<T: ?Sized> Unpin for AlwaysUnpin<T> {} }