commit | bc22a9a4d170216889ae19f74ee663e8ba4fd51e | [log] [tgz] |
---|---|---|
author | James Farrell <[email protected]> | Wed Aug 28 22:55:07 2024 +0000 |
committer | James Farrell <[email protected]> | Wed Aug 28 22:55:07 2024 +0000 |
tree | 925167eb4fd924ab2cd5617287d5872a19ad03bc | |
parent | 86956f42bccf634305d63939230c825c95fef001 [diff] |
Migrate 25 crates to monorepo. pin-project pin-project-internal pin-project-lite pkcs1 pkcs8 plotters-backend plotters-svg ppv-lite86 predicates-core predicates-tree prettyplease proc-macro2 rand rand_chacha rand_core rand_xorshift rayon rayon-core regex regex-syntax rusqlite rustc-demangle rustc-hash rustversion scopeguard Bug: 339424309 Test: treehugger Change-Id: I7daeabab538a9db9f0ecfe7bbff983efe71ef7b8
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.