Mark ab/6881855 as merged

Bug: 172690556
Change-Id: I1b84eb3982849263ff20e1775f0a8f4f963c1e9c
tree: 62e1173064643de076d5710596b3cd6d390f4d00
  1. .github/
  2. examples/
  3. patches/
  4. scripts/
  5. src/
  6. tests/
  7. .cargo_vcs_info.json
  8. .editorconfig
  9. .gitattributes
  10. .gitignore
  11. Android.bp
  12. Cargo.toml
  13. Cargo.toml.orig
  14. CHANGELOG.md
  15. LICENSE-APACHE
  16. LICENSE-MIT
  17. METADATA
  18. MODULE_LICENSE_APACHE2
  19. OWNERS
  20. README.md
  21. rustfmt.toml
  22. TEST_MAPPING
README.md

pin-project

crates-badge docs-badge license-badge rustc-badge

A crate for safe and ergonomic pin-projection.

Usage

Add this to your Cargo.toml:

[dependencies]
pin-project = "1"

The current pin-project requires Rust 1.37 or later.

Examples

#[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.

Related Projects

  • pin-project-lite: A lightweight version of pin-project written with declarative macros.

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.

Contribution

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.