commit | 0522b74a7fee23b223e955de4496d6e529fb504d | [log] [tgz] |
---|---|---|
author | Jeff Vander Stoep <jeffv@google.com> | Thu Nov 05 17:41:44 2020 +0000 |
committer | Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com> | Thu Nov 05 17:41:44 2020 +0000 |
tree | 965721ef9d81e1cdb1b4b0c7b0634a77775c4cad | |
parent | e47a754c92655f184183d23908c1cd767b78e5c0 [diff] | |
parent | bebcd00c5806cf0800d40c97098db894502a32ed [diff] |
TEST_MAPPING: test dependers of this crate am: cd6b1713c1 am: ed02006a6c am: 6b0315fd18 am: bebcd00c58 Original change: https://android-review.googlesource.com/c/platform/external/rust/crates/pin-project/+/1488755 Change-Id: Ibc2bf131af6fb8e7d7cdc27a0c8a0c5dc7fe9c9f
A crate for safe and ergonomic pin-projection.
Add this to your Cargo.toml
:
[dependencies] pin-project = "1"
The current pin-project requires Rust 1.37 or later.
#[pin_project]
attribute creates projection types covering all the fields of struct or enum.
use pin_project::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 } }
code like this will be generated
To use #[pin_project]
on enums, you need to name the projection type returned from the method.
use pin_project::pin_project; use std::pin::Pin; #[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 documentation for more details, and see examples directory for more examples and generated code.
Licensed under either of
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.