- Notifications
You must be signed in to change notification settings - Fork34
A crate for safe and ergonomic pin-projection.
License
Apache-2.0, MIT licenses found
Licenses found
taiki-e/pin-project
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
A crate for safe and ergonomicpin-projection.
Add this to yourCargo.toml
:
[dependencies]pin-project ="1"
#[pin_project]
attribute creates projection typescovering all the fields of struct or enum.
use std::pin::Pin;use pin_project::pin_project;#[pin_project]structStruct<T,U>{#[pin]pinned:T,unpinned:U,}impl<T,U>Struct<T,U>{fnmethod(self:Pin<&mutSelf>){let this =self.project();let _:Pin<&mutT> = this.pinned;// Pinned reference to the fieldlet _:&mutU = this.unpinned;// Normal reference to the field}}
code like this will be generated
To use#[pin_project]
on enums, you need to name the projection typereturned from the method.
use std::pin::Pin;use pin_project::pin_project;#[pin_project(project =EnumProj)]enumEnum<T,U>{Pinned(#[pin]T),Unpinned(U),}impl<T,U>Enum<T,U>{fnmethod(self:Pin<&mutSelf>){matchself.project(){EnumProj::Pinned(x) =>{let _:Pin<&mutT> = x;}EnumProj::Unpinned(y) =>{let _:&mutU = y;}}}}
code like this will be generated
See#[pin_project]
attribute for more details, andseeexamples directory for more examples and generated code.
- pin-project-lite: A lightweight version of pin-project written with declarative macros.
Licensed under either ofApache License, Version 2.0 orMIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submittedfor inclusion in the work by you, as defined in the Apache-2.0 license, shallbe dual licensed as above, without any additional terms or conditions.
About
A crate for safe and ergonomic pin-projection.