From 0ebf5bdf043a27fd3dfb7f92e0cb63d88954c44d Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Fri, 19 Apr 2024 03:47:29 +0200 Subject: Adding upstream version 115.8.0esr. Signed-off-by: Daniel Baumann --- third_party/rust/pin-project/examples/README.md | 39 ++++++ .../pin-project/examples/enum-default-expanded.rs | 101 +++++++++++++ .../rust/pin-project/examples/enum-default.rs | 13 ++ .../pin-project/examples/not_unpin-expanded.rs | 125 +++++++++++++++++ third_party/rust/pin-project/examples/not_unpin.rs | 17 +++ .../pin-project/examples/pinned_drop-expanded.rs | 154 ++++++++++++++++++++ .../rust/pin-project/examples/pinned_drop.rs | 23 +++ .../examples/project_replace-expanded.rs | 156 +++++++++++++++++++++ .../rust/pin-project/examples/project_replace.rs | 14 ++ .../examples/struct-default-expanded.rs | 155 ++++++++++++++++++++ .../rust/pin-project/examples/struct-default.rs | 14 ++ .../pin-project/examples/unsafe_unpin-expanded.rs | 107 ++++++++++++++ .../rust/pin-project/examples/unsafe_unpin.rs | 16 +++ 13 files changed, 934 insertions(+) create mode 100644 third_party/rust/pin-project/examples/README.md create mode 100644 third_party/rust/pin-project/examples/enum-default-expanded.rs create mode 100644 third_party/rust/pin-project/examples/enum-default.rs create mode 100644 third_party/rust/pin-project/examples/not_unpin-expanded.rs create mode 100644 third_party/rust/pin-project/examples/not_unpin.rs create mode 100644 third_party/rust/pin-project/examples/pinned_drop-expanded.rs create mode 100644 third_party/rust/pin-project/examples/pinned_drop.rs create mode 100644 third_party/rust/pin-project/examples/project_replace-expanded.rs create mode 100644 third_party/rust/pin-project/examples/project_replace.rs create mode 100644 third_party/rust/pin-project/examples/struct-default-expanded.rs create mode 100644 third_party/rust/pin-project/examples/struct-default.rs create mode 100644 third_party/rust/pin-project/examples/unsafe_unpin-expanded.rs create mode 100644 third_party/rust/pin-project/examples/unsafe_unpin.rs (limited to 'third_party/rust/pin-project/examples') diff --git a/third_party/rust/pin-project/examples/README.md b/third_party/rust/pin-project/examples/README.md new file mode 100644 index 0000000000..0f30a7fc82 --- /dev/null +++ b/third_party/rust/pin-project/examples/README.md @@ -0,0 +1,39 @@ +# Examples and generated code of each feature of pin-project + +### Basic usage of `#[pin_project]` on structs + +- [example](struct-default.rs) +- [generated code](struct-default-expanded.rs) + +### Basic usage of `#[pin_project]` on enums + +- [example](enum-default.rs) +- [generated code](enum-default-expanded.rs) + +### Manual implementation of `Unpin` by `UnsafeUnpin` + +- [example](unsafe_unpin.rs) +- [generated code](unsafe_unpin-expanded.rs) +- [`UnsafeUnpin` documentation](https://docs.rs/pin-project/1/pin_project/trait.UnsafeUnpin.html) + +### Manual implementation of `Drop` by `#[pinned_drop]` + +- [example](pinned_drop.rs) +- [generated code](pinned_drop-expanded.rs) +- [`#[pinned_drop]` documentation](https://docs.rs/pin-project/1/pin_project/attr.pinned_drop.html) + +### `project_replace()` method + +- [example](project_replace.rs) +- [generated code](project_replace-expanded.rs) +- [`project_replace()` documentation](https://docs.rs/pin-project/1/pin_project/attr.pin_project.html#project_replace-method) + +### Ensure `!Unpin` by `#[pin_project(!Unpin)]` + +- [example](not_unpin.rs) +- [generated code](not_unpin-expanded.rs) +- [`!Unpin` documentation](https://docs.rs/pin-project/1/pin_project/attr.pin_project.html#unpin) + +Note: These generated code examples are the little simplified version of the +actual generated code. See [expansion tests](../tests#expansion-tests-expand-expandtestrs) if you +want to see the exact version of the actual generated code. diff --git a/third_party/rust/pin-project/examples/enum-default-expanded.rs b/third_party/rust/pin-project/examples/enum-default-expanded.rs new file mode 100644 index 0000000000..459ca39b03 --- /dev/null +++ b/third_party/rust/pin-project/examples/enum-default-expanded.rs @@ -0,0 +1,101 @@ +// Original code (./enum-default.rs): +// +// ```rust +// #![allow(dead_code)] +// +// use pin_project::pin_project; +// +// #[pin_project(project = EnumProj)] +// enum Enum { +// Pinned(#[pin] T), +// Unpinned(U), +// } +// +// fn main() {} +// ``` + +#![allow(dead_code, unused_imports, unused_parens, unknown_lints, renamed_and_removed_lints)] +#![allow( + clippy::needless_lifetimes, + clippy::just_underscores_and_digits, + clippy::used_underscore_binding +)] + +use pin_project::pin_project; + +// #[pin_project(project = EnumProj)] +enum Enum { + Pinned(/* #[pin] */ T), + Unpinned(U), +} + +enum EnumProj<'pin, T, U> +where + Enum: 'pin, +{ + Pinned(::pin_project::__private::Pin<&'pin mut (T)>), + Unpinned(&'pin mut (U)), +} + +const _: () = { + // When `#[pin_project]` is used on enums, only named projection types and + // methods are generated because there is no way to access variants of + // projected types without naming it. + // (When `#[pin_project]` is used on structs, both methods are always generated.) + + impl Enum { + fn project<'pin>( + self: ::pin_project::__private::Pin<&'pin mut Self>, + ) -> EnumProj<'pin, T, U> { + unsafe { + match self.get_unchecked_mut() { + Self::Pinned(_0) => { + EnumProj::Pinned(::pin_project::__private::Pin::new_unchecked(_0)) + } + Self::Unpinned(_0) => EnumProj::Unpinned(_0), + } + } + } + } + + // Automatically create the appropriate conditional `Unpin` implementation. + // + // See ./struct-default-expanded.rs and https://github.com/taiki-e/pin-project/pull/53. + // for details. + struct __Enum<'pin, T, U> { + __pin_project_use_generics: ::pin_project::__private::AlwaysUnpin< + 'pin, + (::pin_project::__private::PhantomData, ::pin_project::__private::PhantomData), + >, + __field0: T, + } + impl<'pin, T, U> ::pin_project::__private::Unpin for Enum where + __Enum<'pin, T, U>: ::pin_project::__private::Unpin + { + } + // A dummy impl of `UnsafeUnpin`, to ensure that the user cannot implement it. + #[doc(hidden)] + unsafe impl<'pin, T, U> ::pin_project::UnsafeUnpin for Enum where + __Enum<'pin, T, U>: ::pin_project::__private::Unpin + { + } + + // Ensure that enum does not implement `Drop`. + // + // See ./struct-default-expanded.rs for details. + trait EnumMustNotImplDrop {} + #[allow(clippy::drop_bounds, drop_bounds)] + impl EnumMustNotImplDrop for T {} + impl EnumMustNotImplDrop for Enum {} + // A dummy impl of `PinnedDrop`, to ensure that users don't accidentally + // write a non-functional `PinnedDrop` impls. + #[doc(hidden)] + impl ::pin_project::__private::PinnedDrop for Enum { + unsafe fn drop(self: ::pin_project::__private::Pin<&mut Self>) {} + } + + // We don't need to check for `#[repr(packed)]`, + // since it does not apply to enums. +}; + +fn main() {} diff --git a/third_party/rust/pin-project/examples/enum-default.rs b/third_party/rust/pin-project/examples/enum-default.rs new file mode 100644 index 0000000000..bd3f2e636c --- /dev/null +++ b/third_party/rust/pin-project/examples/enum-default.rs @@ -0,0 +1,13 @@ +// See ./enum-default-expanded.rs for generated code. + +#![allow(dead_code)] + +use pin_project::pin_project; + +#[pin_project(project = EnumProj)] +enum Enum { + Pinned(#[pin] T), + Unpinned(U), +} + +fn main() {} diff --git a/third_party/rust/pin-project/examples/not_unpin-expanded.rs b/third_party/rust/pin-project/examples/not_unpin-expanded.rs new file mode 100644 index 0000000000..5700c120a5 --- /dev/null +++ b/third_party/rust/pin-project/examples/not_unpin-expanded.rs @@ -0,0 +1,125 @@ +// Original code (./not_unpin.rs): +// +// ```rust +// #![allow(dead_code)] +// +// use pin_project::pin_project; +// +// #[pin_project(!Unpin)] +// pub struct Struct { +// #[pin] +// pinned: T, +// unpinned: U, +// } +// +// fn main() { +// fn _is_unpin() {} +// // _is_unpin::>(); //~ ERROR `std::marker::PhantomPinned` cannot be unpinned +// } +// ``` + +#![allow(dead_code, unused_imports, unused_parens, unknown_lints, renamed_and_removed_lints)] +#![allow(clippy::needless_lifetimes)] + +use pin_project::pin_project; + +// #[pin_project(!Unpin)] +pub struct Struct { + // #[pin] + pinned: T, + unpinned: U, +} + +const _: () = { + pub(crate) struct __StructProjection<'pin, T, U> + where + Struct: 'pin, + { + pinned: ::pin_project::__private::Pin<&'pin mut (T)>, + unpinned: &'pin mut (U), + } + pub(crate) struct __StructProjectionRef<'pin, T, U> + where + Struct: 'pin, + { + pinned: ::pin_project::__private::Pin<&'pin (T)>, + unpinned: &'pin (U), + } + + impl Struct { + pub(crate) fn project<'pin>( + self: ::pin_project::__private::Pin<&'pin mut Self>, + ) -> __StructProjection<'pin, T, U> { + unsafe { + let Self { pinned, unpinned } = self.get_unchecked_mut(); + __StructProjection { + pinned: ::pin_project::__private::Pin::new_unchecked(pinned), + unpinned, + } + } + } + pub(crate) fn project_ref<'pin>( + self: ::pin_project::__private::Pin<&'pin Self>, + ) -> __StructProjectionRef<'pin, T, U> { + unsafe { + let Self { pinned, unpinned } = self.get_ref(); + __StructProjectionRef { + pinned: ::pin_project::__private::Pin::new_unchecked(pinned), + unpinned, + } + } + } + } + + // Ensure that it's impossible to use pin projections on a #[repr(packed)] + // struct. + // + // See ./struct-default-expanded.rs and https://github.com/taiki-e/pin-project/pull/34 + // for details. + #[forbid(unaligned_references, safe_packed_borrows)] + fn __assert_not_repr_packed(this: &Struct) { + let _ = &this.pinned; + let _ = &this.unpinned; + } + + // Create `Unpin` impl that has trivial `Unpin` bounds. + // + // See https://github.com/taiki-e/pin-project/issues/102#issuecomment-540472282 + // for details. + impl<'pin, T, U> ::pin_project::__private::Unpin for Struct where + ::pin_project::__private::Wrapper<'pin, ::pin_project::__private::PhantomPinned>: + ::pin_project::__private::Unpin + { + } + // A dummy impl of `UnsafeUnpin`, to ensure that the user cannot implement it. + // + // To ensure that users don't accidentally write a non-functional `UnsafeUnpin` + // impls, we emit one ourselves. If the user ends up writing an `UnsafeUnpin` + // impl, they'll get a "conflicting implementations of trait" error when + // coherence checks are run. + #[doc(hidden)] + unsafe impl<'pin, T, U> ::pin_project::UnsafeUnpin for Struct where + ::pin_project::__private::Wrapper<'pin, ::pin_project::__private::PhantomPinned>: + ::pin_project::__private::Unpin + { + } + + // Ensure that struct does not implement `Drop`. + // + // See ./struct-default-expanded.rs for details. + trait StructMustNotImplDrop {} + #[allow(clippy::drop_bounds, drop_bounds)] + impl StructMustNotImplDrop for T {} + impl StructMustNotImplDrop for Struct {} + // A dummy impl of `PinnedDrop`, to ensure that users don't accidentally + // write a non-functional `PinnedDrop` impls. + #[doc(hidden)] + impl ::pin_project::__private::PinnedDrop for Struct { + unsafe fn drop(self: ::pin_project::__private::Pin<&mut Self>) {} + } +}; + +fn main() { + fn _is_unpin() {} + // _is_unpin::>(); //~ ERROR `std::marker::PhantomPinned` cannot be unpinned +} diff --git a/third_party/rust/pin-project/examples/not_unpin.rs b/third_party/rust/pin-project/examples/not_unpin.rs new file mode 100644 index 0000000000..2ad72a87f2 --- /dev/null +++ b/third_party/rust/pin-project/examples/not_unpin.rs @@ -0,0 +1,17 @@ +// See ./not_unpin-expanded.rs for generated code. + +#![allow(dead_code)] + +use pin_project::pin_project; + +#[pin_project(!Unpin)] +pub struct Struct { + #[pin] + pinned: T, + unpinned: U, +} + +fn main() { + fn _is_unpin() {} + // _is_unpin::>(); //~ ERROR `std::marker::PhantomPinned` cannot be unpinned +} diff --git a/third_party/rust/pin-project/examples/pinned_drop-expanded.rs b/third_party/rust/pin-project/examples/pinned_drop-expanded.rs new file mode 100644 index 0000000000..82207b60b1 --- /dev/null +++ b/third_party/rust/pin-project/examples/pinned_drop-expanded.rs @@ -0,0 +1,154 @@ +// Original code (./pinned_drop.rs): +// +// ```rust +// #![allow(dead_code)] +// +// use std::pin::Pin; +// +// use pin_project::{pin_project, pinned_drop}; +// +// #[pin_project(PinnedDrop)] +// pub struct Struct<'a, T> { +// was_dropped: &'a mut bool, +// #[pin] +// field: T, +// } +// +// #[pinned_drop] +// fn drop_Struct(mut this: Pin<&mut Struct<'_, T>>) { +// **this.project().was_dropped = true; +// } +// +// fn main() {} +// ``` + +#![allow(dead_code, unused_imports, unused_parens, unknown_lints, renamed_and_removed_lints)] +#![allow(clippy::needless_lifetimes, clippy::mut_mut)] + +use std::pin::Pin; + +use pin_project::{pin_project, pinned_drop}; + +// #[pin_project(PinnedDrop)] +pub struct Struct<'a, T> { + was_dropped: &'a mut bool, + // #[pin] + field: T, +} + +const _: () = { + pub(crate) struct __StructProjection<'pin, 'a, T> + where + Struct<'a, T>: 'pin, + { + was_dropped: &'pin mut (&'a mut bool), + field: ::pin_project::__private::Pin<&'pin mut (T)>, + } + pub(crate) struct __StructProjectionRef<'pin, 'a, T> + where + Struct<'a, T>: 'pin, + { + was_dropped: &'pin (&'a mut bool), + field: ::pin_project::__private::Pin<&'pin (T)>, + } + + impl<'a, T> Struct<'a, T> { + pub(crate) fn project<'pin>( + self: ::pin_project::__private::Pin<&'pin mut Self>, + ) -> __StructProjection<'pin, 'a, T> { + unsafe { + let Self { was_dropped, field } = self.get_unchecked_mut(); + __StructProjection { + was_dropped, + field: ::pin_project::__private::Pin::new_unchecked(field), + } + } + } + pub(crate) fn project_ref<'pin>( + self: ::pin_project::__private::Pin<&'pin Self>, + ) -> __StructProjectionRef<'pin, 'a, T> { + unsafe { + let Self { was_dropped, field } = self.get_ref(); + __StructProjectionRef { + was_dropped, + field: ::pin_project::__private::Pin::new_unchecked(field), + } + } + } + } + + // Ensure that it's impossible to use pin projections on a #[repr(packed)] + // struct. + // + // See ./struct-default-expanded.rs and https://github.com/taiki-e/pin-project/pull/34 + // for details. + #[forbid(unaligned_references, safe_packed_borrows)] + fn __assert_not_repr_packed<'a, T>(this: &Struct<'a, T>) { + let _ = &this.was_dropped; + let _ = &this.field; + } + + impl<'a, T> ::pin_project::__private::Drop for Struct<'a, T> { + fn drop(&mut self) { + // Safety - we're in 'drop', so we know that 'self' will + // never move again. + let pinned_self = unsafe { ::pin_project::__private::Pin::new_unchecked(self) }; + // We call `pinned_drop` only once. Since `PinnedDrop::drop` + // is an unsafe method and a private API, it is never called again in safe + // code *unless the user uses a maliciously crafted macro*. + unsafe { + ::pin_project::__private::PinnedDrop::drop(pinned_self); + } + } + } + + // Automatically create the appropriate conditional `Unpin` implementation. + // + // See ./struct-default-expanded.rs and https://github.com/taiki-e/pin-project/pull/53. + // for details. + pub struct __Struct<'pin, 'a, T> { + __pin_project_use_generics: ::pin_project::__private::AlwaysUnpin<'pin, (T)>, + __field0: T, + __lifetime0: &'a (), + } + impl<'pin, 'a, T> ::pin_project::__private::Unpin for Struct<'a, T> where + __Struct<'pin, 'a, T>: ::pin_project::__private::Unpin + { + } + // A dummy impl of `UnsafeUnpin`, to ensure that the user cannot implement it. + #[doc(hidden)] + unsafe impl<'pin, 'a, T> ::pin_project::UnsafeUnpin for Struct<'a, T> where + __Struct<'pin, 'a, T>: ::pin_project::__private::Unpin + { + } +}; + +// Implementing `PinnedDrop::drop` is safe, but calling it is not safe. +// This is because destructors can be called multiple times in safe code and +// [double dropping is unsound](https://github.com/rust-lang/rust/pull/62360). +// +// Ideally, it would be desirable to be able to forbid manual calls in +// the same way as `Drop::drop`, but the library cannot do it. So, by using +// macros and replacing them with private traits, we prevent users from +// calling `PinnedDrop::drop`. +// +// Users can implement [`Drop`] safely using `#[pinned_drop]` and can drop a +// type that implements `PinnedDrop` using the [`drop`] function safely. +// **Do not call or implement this trait directly.** +#[doc(hidden)] +impl ::pin_project::__private::PinnedDrop for Struct<'_, T> { + // Since calling it twice on the same object would be UB, + // this method is unsafe. + unsafe fn drop(self: Pin<&mut Self>) { + #[allow(clippy::needless_pass_by_value)] + fn __drop_inner(__self: Pin<&mut Struct<'_, T>>) { + // A dummy `__drop_inner` function to prevent users call outer `__drop_inner`. + fn __drop_inner() {} + + **__self.project().was_dropped = true; + } + __drop_inner(self); + } +} + +fn main() {} diff --git a/third_party/rust/pin-project/examples/pinned_drop.rs b/third_party/rust/pin-project/examples/pinned_drop.rs new file mode 100644 index 0000000000..6fb87193f6 --- /dev/null +++ b/third_party/rust/pin-project/examples/pinned_drop.rs @@ -0,0 +1,23 @@ +// See ./pinned_drop-expanded.rs for generated code. + +#![allow(dead_code)] + +use std::pin::Pin; + +use pin_project::{pin_project, pinned_drop}; + +#[pin_project(PinnedDrop)] +pub struct Struct<'a, T> { + was_dropped: &'a mut bool, + #[pin] + field: T, +} + +#[pinned_drop] +impl PinnedDrop for Struct<'_, T> { + fn drop(self: Pin<&mut Self>) { + **self.project().was_dropped = true; + } +} + +fn main() {} diff --git a/third_party/rust/pin-project/examples/project_replace-expanded.rs b/third_party/rust/pin-project/examples/project_replace-expanded.rs new file mode 100644 index 0000000000..445e5705e1 --- /dev/null +++ b/third_party/rust/pin-project/examples/project_replace-expanded.rs @@ -0,0 +1,156 @@ +// Original code (./struct-default.rs): +// +// ```rust +// #![allow(dead_code)] +// +// use pin_project::pin_project; +// +// #[pin_project(project_replace)] +// struct Struct { +// #[pin] +// pinned: T, +// unpinned: U, +// } +// +// fn main() {} +// ``` + +#![allow(dead_code, unused_imports, unused_parens, unknown_lints, renamed_and_removed_lints)] +#![allow(clippy::needless_lifetimes)] + +use pin_project::pin_project; + +// #[pin_project(project_replace)] +struct Struct { + // #[pin] + pinned: T, + unpinned: U, +} + +const _: () = { + struct __StructProjection<'pin, T, U> + where + Struct: 'pin, + { + pinned: ::pin_project::__private::Pin<&'pin mut (T)>, + unpinned: &'pin mut (U), + } + struct __StructProjectionRef<'pin, T, U> + where + Struct: 'pin, + { + pinned: ::pin_project::__private::Pin<&'pin (T)>, + unpinned: &'pin (U), + } + struct __StructProjectionOwned { + pinned: ::pin_project::__private::PhantomData, + unpinned: U, + } + + impl Struct { + fn project<'pin>( + self: ::pin_project::__private::Pin<&'pin mut Self>, + ) -> __StructProjection<'pin, T, U> { + unsafe { + let Self { pinned, unpinned } = self.get_unchecked_mut(); + __StructProjection { + pinned: ::pin_project::__private::Pin::new_unchecked(pinned), + unpinned, + } + } + } + fn project_ref<'pin>( + self: ::pin_project::__private::Pin<&'pin Self>, + ) -> __StructProjectionRef<'pin, T, U> { + unsafe { + let Self { pinned, unpinned } = self.get_ref(); + __StructProjectionRef { + pinned: ::pin_project::__private::Pin::new_unchecked(pinned), + unpinned, + } + } + } + fn project_replace( + self: ::pin_project::__private::Pin<&mut Self>, + __replacement: Self, + ) -> __StructProjectionOwned { + unsafe { + let __self_ptr: *mut Self = self.get_unchecked_mut(); + + // Destructors will run in reverse order, so next create a guard to overwrite + // `self` with the replacement value without calling destructors. + let __guard = + ::pin_project::__private::UnsafeOverwriteGuard::new(__self_ptr, __replacement); + + let Self { pinned, unpinned } = &mut *__self_ptr; + + // First, extract all the unpinned fields + let __result = __StructProjectionOwned { + pinned: ::pin_project::__private::PhantomData, + unpinned: ::pin_project::__private::ptr::read(unpinned), + }; + + // Now create guards to drop all the pinned fields + // + // Due to a compiler bug (https://github.com/rust-lang/rust/issues/47949) + // this must be in its own scope, or else `__result` will not be dropped + // if any of the destructors panic. + { + let __guard = ::pin_project::__private::UnsafeDropInPlaceGuard::new(pinned); + } + + // Finally, return the result + __result + } + } + } + + // Ensure that it's impossible to use pin projections on a #[repr(packed)] + // struct. + // + // See ./struct-default-expanded.rs and https://github.com/taiki-e/pin-project/pull/34 + // for details. + #[forbid(unaligned_references, safe_packed_borrows)] + fn __assert_not_repr_packed(this: &Struct) { + let _ = &this.pinned; + let _ = &this.unpinned; + } + + // Automatically create the appropriate conditional `Unpin` implementation. + // + // See ./struct-default-expanded.rs and https://github.com/taiki-e/pin-project/pull/53. + // for details. + struct __Struct<'pin, T, U> { + __pin_project_use_generics: ::pin_project::__private::AlwaysUnpin< + 'pin, + (::pin_project::__private::PhantomData, ::pin_project::__private::PhantomData), + >, + __field0: T, + } + impl<'pin, T, U> ::pin_project::__private::Unpin for Struct where + __Struct<'pin, T, U>: ::pin_project::__private::Unpin + { + } + // A dummy impl of `UnsafeUnpin`, to ensure that the user cannot implement it. + #[doc(hidden)] + unsafe impl<'pin, T, U> ::pin_project::UnsafeUnpin for Struct where + __Struct<'pin, T, U>: ::pin_project::__private::Unpin + { + } + + // Ensure that struct does not implement `Drop`. + // + // See ./struct-default-expanded.rs for details. + trait StructMustNotImplDrop {} + #[allow(clippy::drop_bounds, drop_bounds)] + impl StructMustNotImplDrop for T {} + impl StructMustNotImplDrop for Struct {} + // A dummy impl of `PinnedDrop`, to ensure that users don't accidentally + // write a non-functional `PinnedDrop` impls. + #[doc(hidden)] + impl ::pin_project::__private::PinnedDrop for Struct { + unsafe fn drop(self: ::pin_project::__private::Pin<&mut Self>) {} + } +}; + +fn main() {} diff --git a/third_party/rust/pin-project/examples/project_replace.rs b/third_party/rust/pin-project/examples/project_replace.rs new file mode 100644 index 0000000000..99cec18472 --- /dev/null +++ b/third_party/rust/pin-project/examples/project_replace.rs @@ -0,0 +1,14 @@ +// See ./struct-default-expanded.rs for generated code. + +#![allow(dead_code)] + +use pin_project::pin_project; + +#[pin_project(project_replace)] +struct Struct { + #[pin] + pinned: T, + unpinned: U, +} + +fn main() {} diff --git a/third_party/rust/pin-project/examples/struct-default-expanded.rs b/third_party/rust/pin-project/examples/struct-default-expanded.rs new file mode 100644 index 0000000000..d6610993d3 --- /dev/null +++ b/third_party/rust/pin-project/examples/struct-default-expanded.rs @@ -0,0 +1,155 @@ +// Original code (./struct-default.rs): +// +// ```rust +// #![allow(dead_code)] +// +// use pin_project::pin_project; +// +// #[pin_project] +// struct Struct { +// #[pin] +// pinned: T, +// unpinned: U, +// } +// +// fn main() {} +// ``` + +#![allow(dead_code, unused_imports, unused_parens, unknown_lints, renamed_and_removed_lints)] +#![allow(clippy::needless_lifetimes)] + +use pin_project::pin_project; + +// #[pin_project] +struct Struct { + // #[pin] + pinned: T, + unpinned: U, +} + +const _: () = { + struct __StructProjection<'pin, T, U> + where + Struct: 'pin, + { + pinned: ::pin_project::__private::Pin<&'pin mut (T)>, + unpinned: &'pin mut (U), + } + struct __StructProjectionRef<'pin, T, U> + where + Struct: 'pin, + { + pinned: ::pin_project::__private::Pin<&'pin (T)>, + unpinned: &'pin (U), + } + + impl Struct { + fn project<'pin>( + self: ::pin_project::__private::Pin<&'pin mut Self>, + ) -> __StructProjection<'pin, T, U> { + unsafe { + let Self { pinned, unpinned } = self.get_unchecked_mut(); + __StructProjection { + pinned: ::pin_project::__private::Pin::new_unchecked(pinned), + unpinned, + } + } + } + fn project_ref<'pin>( + self: ::pin_project::__private::Pin<&'pin Self>, + ) -> __StructProjectionRef<'pin, T, U> { + unsafe { + let Self { pinned, unpinned } = self.get_ref(); + __StructProjectionRef { + pinned: ::pin_project::__private::Pin::new_unchecked(pinned), + unpinned, + } + } + } + } + + // Ensure that it's impossible to use pin projections on a #[repr(packed)] + // struct. + // + // Taking a reference to a packed field is UB, and applying + // `#[forbid(unaligned_references)]` makes sure that doing this 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(unaligned_references, safe_packed_borrows)] + fn __assert_not_repr_packed(this: &Struct) { + let _ = &this.pinned; + let _ = &this.unpinned; + } + + // Automatically create the appropriate conditional `Unpin` implementation. + // + // Basically this is equivalent to the following code: + // + // ```rust + // impl Unpin for Struct 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 declared + // within a function, which makes it impossible to be named by user code. + // This guarantees 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', satisfying 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. + struct __Struct<'pin, T, U> { + __pin_project_use_generics: ::pin_project::__private::AlwaysUnpin< + 'pin, + (::pin_project::__private::PhantomData, ::pin_project::__private::PhantomData), + >, + __field0: T, + } + impl<'pin, T, U> ::pin_project::__private::Unpin for Struct where + __Struct<'pin, T, U>: ::pin_project::__private::Unpin + { + } + // A dummy impl of `UnsafeUnpin`, to ensure that the user cannot implement it. + // + // To ensure that users don't accidentally write a non-functional `UnsafeUnpin` + // impls, we emit one ourselves. If the user ends up writing an `UnsafeUnpin` + // impl, they'll get a "conflicting implementations of trait" error when + // coherence checks are run. + #[doc(hidden)] + unsafe impl<'pin, T, U> ::pin_project::UnsafeUnpin for Struct where + __Struct<'pin, T, U>: ::pin_project::__private::Unpin + { + } + + // Ensure that struct does not implement `Drop`. + // + // If you attempt to provide an Drop impl, the blanket impl will + // then apply to your type, causing a compile-time error due to + // the conflict with the second impl. + trait StructMustNotImplDrop {} + #[allow(clippy::drop_bounds, drop_bounds)] + impl StructMustNotImplDrop for T {} + impl StructMustNotImplDrop for Struct {} + // A dummy impl of `PinnedDrop`, to ensure that users don't accidentally + // write a non-functional `PinnedDrop` impls. + #[doc(hidden)] + impl ::pin_project::__private::PinnedDrop for Struct { + unsafe fn drop(self: ::pin_project::__private::Pin<&mut Self>) {} + } +}; + +fn main() {} diff --git a/third_party/rust/pin-project/examples/struct-default.rs b/third_party/rust/pin-project/examples/struct-default.rs new file mode 100644 index 0000000000..46808a5827 --- /dev/null +++ b/third_party/rust/pin-project/examples/struct-default.rs @@ -0,0 +1,14 @@ +// See ./struct-default-expanded.rs for generated code. + +#![allow(dead_code)] + +use pin_project::pin_project; + +#[pin_project] +struct Struct { + #[pin] + pinned: T, + unpinned: U, +} + +fn main() {} diff --git a/third_party/rust/pin-project/examples/unsafe_unpin-expanded.rs b/third_party/rust/pin-project/examples/unsafe_unpin-expanded.rs new file mode 100644 index 0000000000..e9c7abcedd --- /dev/null +++ b/third_party/rust/pin-project/examples/unsafe_unpin-expanded.rs @@ -0,0 +1,107 @@ +// Original code (./unsafe_unpin.rs): +// +// ```rust +// #![allow(dead_code)] +// +// use pin_project::{pin_project, UnsafeUnpin}; +// +// #[pin_project(UnsafeUnpin)] +// pub struct Struct { +// #[pin] +// pinned: T, +// unpinned: U, +// } +// +// unsafe impl UnsafeUnpin for Struct {} +// +// fn main() {} +// ``` + +#![allow(dead_code, unused_imports, unused_parens, unknown_lints, renamed_and_removed_lints)] +#![allow(clippy::needless_lifetimes)] + +use pin_project::{pin_project, UnsafeUnpin}; + +// #[pin_project(UnsafeUnpin)] +pub struct Struct { + // #[pin] + pinned: T, + unpinned: U, +} + +const _: () = { + pub(crate) struct __StructProjection<'pin, T, U> + where + Struct: 'pin, + { + pinned: ::pin_project::__private::Pin<&'pin mut (T)>, + unpinned: &'pin mut (U), + } + pub(crate) struct __StructProjectionRef<'pin, T, U> + where + Struct: 'pin, + { + pinned: ::pin_project::__private::Pin<&'pin (T)>, + unpinned: &'pin (U), + } + + impl Struct { + pub(crate) fn project<'pin>( + self: ::pin_project::__private::Pin<&'pin mut Self>, + ) -> __StructProjection<'pin, T, U> { + unsafe { + let Self { pinned, unpinned } = self.get_unchecked_mut(); + __StructProjection { + pinned: ::pin_project::__private::Pin::new_unchecked(pinned), + unpinned, + } + } + } + pub(crate) fn project_ref<'pin>( + self: ::pin_project::__private::Pin<&'pin Self>, + ) -> __StructProjectionRef<'pin, T, U> { + unsafe { + let Self { pinned, unpinned } = self.get_ref(); + __StructProjectionRef { + pinned: ::pin_project::__private::Pin::new_unchecked(pinned), + unpinned, + } + } + } + } + + // Ensure that it's impossible to use pin projections on a #[repr(packed)] + // struct. + // + // See ./struct-default-expanded.rs and https://github.com/taiki-e/pin-project/pull/34 + // for details. + #[forbid(unaligned_references, safe_packed_borrows)] + fn __assert_not_repr_packed(this: &Struct) { + let _ = &this.pinned; + let _ = &this.unpinned; + } + + // Implement `Unpin` via `UnsafeUnpin`. + impl<'pin, T, U> ::pin_project::__private::Unpin for Struct where + ::pin_project::__private::Wrapper<'pin, Self>: ::pin_project::UnsafeUnpin + { + } + + // Ensure that struct does not implement `Drop`. + // + // See ./struct-default-expanded.rs for details. + trait StructMustNotImplDrop {} + #[allow(clippy::drop_bounds, drop_bounds)] + impl StructMustNotImplDrop for T {} + impl StructMustNotImplDrop for Struct {} + // A dummy impl of `PinnedDrop`, to ensure that users don't accidentally + // write a non-functional `PinnedDrop` impls. + #[doc(hidden)] + impl ::pin_project::__private::PinnedDrop for Struct { + unsafe fn drop(self: ::pin_project::__private::Pin<&mut Self>) {} + } +}; + +unsafe impl UnsafeUnpin for Struct {} + +fn main() {} diff --git a/third_party/rust/pin-project/examples/unsafe_unpin.rs b/third_party/rust/pin-project/examples/unsafe_unpin.rs new file mode 100644 index 0000000000..5ec0cd22ba --- /dev/null +++ b/third_party/rust/pin-project/examples/unsafe_unpin.rs @@ -0,0 +1,16 @@ +// See ./unsafe_unpin-expanded.rs for generated code. + +#![allow(dead_code)] + +use pin_project::{pin_project, UnsafeUnpin}; + +#[pin_project(UnsafeUnpin)] +pub struct Struct { + #[pin] + pinned: T, + unpinned: U, +} + +unsafe impl UnsafeUnpin for Struct {} + +fn main() {} -- cgit v1.2.3