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