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