commit | 7bb85295ed2dd9ad11af9b1bd67610eac53cc3fd | [log] [tgz] |
---|---|---|
author | Linux Build Service Account <lnxbuild@localhost> | Mon May 17 12:48:05 2021 -0700 |
committer | Linux Build Service Account <lnxbuild@localhost> | Mon May 17 12:48:05 2021 -0700 |
tree | 33606e3663ed7bfe2c6fdc2fb260ef81d381059e | |
parent | b3f01031d5fa51eec023ecaf1a5310c89939aa33 [diff] | |
parent | 3b9be46dc0e8ff48fe0cdbd4c36495775d366970 [diff] |
Merge 3b9be46dc0e8ff48fe0cdbd4c36495775d366970 on remote branch Change-Id: I2096a20aeb2428d212b9a0ef679380545ac76d7f
A crate for safe and ergonomic pin-projection.
Add this to your Cargo.toml
:
[dependencies] pin-project = "1"
Compiler support: requires rustc 1.37+
#[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 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.