1# pin-project 2 3[](https://crates.io/crates/pin-project) 4[](https://docs.rs/pin-project) 5[](#license) 6[](https://www.rust-lang.org) 7[](https://github.com/taiki-e/pin-project/actions) 8 9<!-- tidy:crate-doc:start --> 10A crate for safe and ergonomic [pin-projection]. 11 12## Usage 13 14Add this to your `Cargo.toml`: 15 16```toml 17[dependencies] 18pin-project = "1" 19``` 20 21*Compiler support: requires rustc 1.56+* 22 23## Examples 24 25[`#[pin_project]`][`pin_project`] attribute creates projection types 26covering all the fields of struct or enum. 27 28```rust 29use std::pin::Pin; 30 31use pin_project::pin_project; 32 33#[pin_project] 34struct Struct<T, U> { 35 #[pin] 36 pinned: T, 37 unpinned: U, 38} 39 40impl<T, U> Struct<T, U> { 41 fn method(self: Pin<&mut Self>) { 42 let this = self.project(); 43 let _: Pin<&mut T> = this.pinned; // Pinned reference to the field 44 let _: &mut U = this.unpinned; // Normal reference to the field 45 } 46} 47``` 48 49[*code like this will be generated*][struct-default-expanded] 50 51To use `#[pin_project]` on enums, you need to name the projection type 52returned from the method. 53 54```rust 55use std::pin::Pin; 56 57use pin_project::pin_project; 58 59#[pin_project(project = EnumProj)] 60enum Enum<T, U> { 61 Pinned(#[pin] T), 62 Unpinned(U), 63} 64 65impl<T, U> Enum<T, U> { 66 fn method(self: Pin<&mut Self>) { 67 match self.project() { 68 EnumProj::Pinned(x) => { 69 let _: Pin<&mut T> = x; 70 } 71 EnumProj::Unpinned(y) => { 72 let _: &mut U = y; 73 } 74 } 75 } 76} 77``` 78 79[*code like this will be generated*][enum-default-expanded] 80 81See [`#[pin_project]`][`pin_project`] attribute for more details, and 82see [examples] directory for more examples and generated code. 83 84## Related Projects 85 86- [pin-project-lite]: A lightweight version of pin-project written with declarative macros. 87 88[enum-default-expanded]: https://github.com/taiki-e/pin-project/blob/HEAD/examples/enum-default-expanded.rs 89[examples]: https://github.com/taiki-e/pin-project/blob/HEAD/examples/README.md 90[pin-project-lite]: https://github.com/taiki-e/pin-project-lite 91[pin-projection]: https://doc.rust-lang.org/std/pin/index.html#projections-and-structural-pinning 92[struct-default-expanded]: https://github.com/taiki-e/pin-project/blob/HEAD/examples/struct-default-expanded.rs 93 94<!-- tidy:crate-doc:end --> 95 96[`pin_project`]: https://docs.rs/pin-project/1/pin_project/attr.pin_project.html 97 98## License 99 100Licensed under either of [Apache License, Version 2.0](LICENSE-APACHE) or 101[MIT license](LICENSE-MIT) at your option. 102 103Unless you explicitly state otherwise, any contribution intentionally submitted 104for inclusion in the work by you, as defined in the Apache-2.0 license, shall 105be dual licensed as above, without any additional terms or conditions. 106