1# pin-project-lite 2 3[![crates.io](https://img.shields.io/crates/v/pin-project-lite.svg?style=flat-square&logo=rust)](https://crates.io/crates/pin-project-lite) 4[![docs.rs](https://img.shields.io/badge/docs.rs-pin--project--lite-blue?style=flat-square)](https://docs.rs/pin-project-lite) 5[![license](https://img.shields.io/badge/license-Apache--2.0_OR_MIT-blue.svg?style=flat-square)](#license) 6[![rustc](https://img.shields.io/badge/rustc-1.37+-blue.svg?style=flat-square)](https://www.rust-lang.org) 7[![build status](https://img.shields.io/github/workflow/status/taiki-e/pin-project-lite/CI/master?style=flat-square)](https://github.com/taiki-e/pin-project-lite/actions?query=workflow%3ACI+branch%3Amaster) 8 9A lightweight version of [pin-project] written with declarative macros. 10 11## Usage 12 13Add this to your `Cargo.toml`: 14 15```toml 16[dependencies] 17pin-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 25struct. 26 27```rust 28use pin_project_lite::pin_project; 29use std::pin::Pin; 30 31pin_project! { 32 struct Struct<T, U> { 33 #[pin] 34 pinned: T, 35 unpinned: U, 36 } 37} 38 39impl<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 48To use [`pin_project!`] on enums, you need to name the projection type 49returned from the method. 50 51```rust 52use pin_project_lite::pin_project; 53use std::pin::Pin; 54 55pin_project! { 56 #[project = EnumProj] 57 enum Enum<T, U> { 58 Variant { #[pin] pinned: T, unpinned: U }, 59 } 60} 61 62impl<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 76Here are some similarities and differences compared to [pin-project]. 77 78### Similar: Safety 79 80pin-project-lite guarantees safety in much the same way as [pin-project]. 81Both are completely safe unless you write other unsafe code. 82 83### Different: Minimal design 84 85This 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 91This is the **only** reason to use this crate. However, **if you already 92have proc-macro related dependencies in your crate's dependency graph, there 93is no benefit from using this crate.** (Note: There is almost no difference 94in the amount of code generated between [pin-project] and pin-project-lite.) 95 96### Different: No useful error messages 97 98This macro does not handle any invalid input. So error messages are not to 99be useful in most cases. If you do need useful error messages, then upon 100error you can pass the same input to [pin-project] to receive a helpful 101description of the compile error. 102 103### Different: No support for custom Drop implementation 104 105pin-project supports this by [`#[pinned_drop]`][pinned-drop]. 106 107### Different: No support for custom Unpin implementation 108 109pin-project supports this by [`UnsafeUnpin`][unsafe-unpin] and [`!Unpin`][not-unpin]. 110 111### Different: No support for tuple structs and tuple variants 112 113pin-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 123Licensed under either of [Apache License, Version 2.0](LICENSE-APACHE) or 124[MIT license](LICENSE-MIT) at your option. 125 126Unless you explicitly state otherwise, any contribution intentionally submitted 127for inclusion in the work by you, as defined in the Apache-2.0 license, shall 128be dual licensed as above, without any additional terms or conditions. 129