| 1 |
# pin-project-lite |
| 2 |
|
| 3 |
[](https://crates.io/crates/pin-project-lite) |
| 4 |
[](https://docs.rs/pin-project-lite) |
| 5 |
[](#license) |
| 6 |
[](https://www.rust-lang.org) |
| 7 |
[](https://github.com/taiki-e/pin-project-lite/actions) |
| 8 |
|
| 9 |
<!-- tidy:sync-markdown-to-rustdoc:start:src/lib.rs --> |
| 10 |
|
| 11 |
A lightweight version of [pin-project] written with declarative macros. |
| 12 |
|
| 13 |
## Usage |
| 14 |
|
| 15 |
Add this to your `Cargo.toml`: |
| 16 |
|
| 17 |
```toml |
| 18 |
[dependencies] |
| 19 |
pin-project-lite = "0.2" |
| 20 |
``` |
| 21 |
|
| 22 |
## Examples |
| 23 |
|
| 24 |
[`pin_project!`] macro creates a projection type covering all the fields of |
| 25 |
struct. |
| 26 |
|
| 27 |
```rust |
| 28 |
use std::pin::Pin; |
| 29 |
|
| 30 |
use pin_project_lite::pin_project; |
| 31 |
|
| 32 |
pin_project! { |
| 33 |
struct Struct<T, U> { |
| 34 |
#[pin] |
| 35 |
pinned: T, |
| 36 |
unpinned: U, |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
impl<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 |
To use [`pin_project!`] on enums, you need to name the projection type |
| 50 |
returned from the method. |
| 51 |
|
| 52 |
```rust |
| 53 |
use std::pin::Pin; |
| 54 |
|
| 55 |
use pin_project_lite::pin_project; |
| 56 |
|
| 57 |
pin_project! { |
| 58 |
#[project = EnumProj] |
| 59 |
enum Enum<T, U> { |
| 60 |
Variant { #[pin] pinned: T, unpinned: U }, |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
impl<T, U> Enum<T, U> { |
| 65 |
fn method(self: Pin<&mut Self>) { |
| 66 |
match self.project() { |
| 67 |
EnumProj::Variant { pinned, unpinned } => { |
| 68 |
let _: Pin<&mut T> = pinned; |
| 69 |
let _: &mut U = unpinned; |
| 70 |
} |
| 71 |
} |
| 72 |
} |
| 73 |
} |
| 74 |
``` |
| 75 |
|
| 76 |
## [pin-project] vs pin-project-lite |
| 77 |
|
| 78 |
Here are some similarities and differences compared to [pin-project]. |
| 79 |
|
| 80 |
### Similar: Safety |
| 81 |
|
| 82 |
pin-project-lite guarantees safety in much the same way as [pin-project]. |
| 83 |
Both are completely safe unless you write other unsafe code. |
| 84 |
|
| 85 |
### Different: Minimal design |
| 86 |
|
| 87 |
This library does not tackle as expansive of a range of use cases as |
| 88 |
[pin-project] does. If your use case is not already covered, please use |
| 89 |
[pin-project]. |
| 90 |
|
| 91 |
### Different: No proc-macro related dependencies |
| 92 |
|
| 93 |
This is the **only** reason to use this crate. However, **if you already |
| 94 |
have proc-macro related dependencies in your crate's dependency graph, there |
| 95 |
is no benefit from using this crate.** (Note: There is almost no difference |
| 96 |
in the amount of code generated between [pin-project] and pin-project-lite.) |
| 97 |
|
| 98 |
### Different: No useful error messages |
| 99 |
|
| 100 |
This macro does not handle any invalid input. So error messages are not to |
| 101 |
be useful in most cases. If you do need useful error messages, then upon |
| 102 |
error you can pass the same input to [pin-project] to receive a helpful |
| 103 |
description of the compile error. |
| 104 |
|
| 105 |
### Different: No support for custom Unpin implementation |
| 106 |
|
| 107 |
pin-project supports this by [`UnsafeUnpin`][unsafe-unpin]. (`!Unpin` is supported by both [pin-project][not-unpin] and [pin-project-lite][not-unpin-lite].) |
| 108 |
|
| 109 |
### Different: No support for tuple structs and tuple variants |
| 110 |
|
| 111 |
pin-project supports this. |
| 112 |
|
| 113 |
[not-unpin]: https://docs.rs/pin-project/latest/pin_project/attr.pin_project.html#unpin |
| 114 |
[pin-project]: https://github.com/taiki-e/pin-project |
| 115 |
[unsafe-unpin]: https://docs.rs/pin-project/latest/pin_project/attr.pin_project.html#unsafeunpin |
| 116 |
|
| 117 |
<!-- tidy:sync-markdown-to-rustdoc:end --> |
| 118 |
|
| 119 |
[not-unpin-lite]: https://docs.rs/pin-project-lite/latest/pin_project_lite/macro.pin_project.html#unpin |
| 120 |
[`pin_project!`]: https://docs.rs/pin-project-lite/latest/pin_project_lite/macro.pin_project.html |
| 121 |
|
| 122 |
## License |
| 123 |
|
| 124 |
Licensed under either of [Apache License, Version 2.0](LICENSE-APACHE) or |
| 125 |
[MIT license](LICENSE-MIT) at your option. |
| 126 |
|
| 127 |
Unless you explicitly state otherwise, any contribution intentionally submitted |
| 128 |
for inclusion in the work by you, as defined in the Apache-2.0 license, shall |
| 129 |
be dual licensed as above, without any additional terms or conditions. |