commit | 1ebf615571682e3c581a4ecae0fa7f23390465ad | [log] [tgz] |
---|---|---|
author | Android Build Coastguard Worker <[email protected]> | Sat May 11 01:13:46 2024 +0000 |
committer | Android Build Coastguard Worker <[email protected]> | Sat May 11 01:13:46 2024 +0000 |
tree | 8974a993893826f1fe3d6270715d6afa1df96e70 | |
parent | 4052a3c23a002a7fa935ed3df487fe97d954b0f6 [diff] | |
parent | 003e8dd346fa39c3f1124cc4aec6c05aa0ef2cef [diff] |
Snap for 11828632 from 003e8dd346fa39c3f1124cc4aec6c05aa0ef2cef to 24Q3-release Change-Id: I37c7999ac0105ffbe34e7d0b525af1a19ec52acf
A crate for safe and ergonomic pin-projection.
Add this to your Cargo.toml
:
[dependencies] pin-project = "1"
Compiler support: requires rustc 1.56+
#[pin_project]
attribute creates projection types covering all the fields of struct or enum.
use std::pin::Pin; use pin_project::pin_project; #[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 } }
code like this will be generated
To use #[pin_project]
on enums, you need to name the projection type returned from the method.
use std::pin::Pin; use pin_project::pin_project; #[pin_project(project = EnumProj)] enum Enum<T, U> { Pinned(#[pin] T), Unpinned(U), } impl<T, U> Enum<T, U> { fn method(self: Pin<&mut Self>) { match self.project() { EnumProj::Pinned(x) => { let _: Pin<&mut T> = x; } EnumProj::Unpinned(y) => { let _: &mut U = y; } } } }
code like this will be generated
See #[pin_project]
attribute for more details, and see examples directory for more examples and generated code.
Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.