#pin #owned #move #rvalue

nightly no-std owned-pin

A wrapper that both owns and pins data in memory

10 releases (stable)

1.3.3 Dec 2, 2023
1.3.1 Dec 1, 2023
1.2.0 Nov 30, 2023
0.2.0 Nov 30, 2023
0.1.1 Nov 30, 2023

#255 in Memory management

Download history 115/week @ 2024-02-16 52/week @ 2024-02-23 13/week @ 2024-03-01 2/week @ 2024-03-08 2/week @ 2024-03-15 57/week @ 2024-03-29 12/week @ 2024-04-05

69 downloads per month



Owned Pin

Cargo Documentation License

This crate deals with data that is owned by some entity but (maybe) immovable in memory. It is inspired by R-value references in C++.

See the documentation for more information.


With Pin<P> only, we cannot guarantee the move semantics of the value.

use core::pin::{Pin, pin};
use core::marker::PhantomPinned;

fn try_to_take_the_ownership_of<T>(pinned: Pin<&mut T>) {}

let mut value = pin!(PhantomPinned);
// The caller can reborrow the value...
// ... so the same pinned data can be used twice,
// thus unable to guarantee the move semantics.

In practice, this is because there is no such smart pointer that owns data by holding a unique reference to some other location on the stack.

Thus, we introduce the OnStack<T> smart pointer and an alias of OPin<T> = Pin<OnStack<T>>, which both "own" and "pin" the data on the stack, enabling the example above to work as desired:

use owned_pin::{OPin, opin};
use core::marker::PhantomPinned;

fn take_the_ownership_of<T>(owned_and_pinned: OPin<T>) {}

let value = opin!(PhantomPinned);
// The `as_mut` method of `OPin` actually
// returns a `Pin<&mut T>`...
// ... so the value itself cannot be used again.
// The line below causes rustc to emit `E0382`.
// take_the_ownership_of(value);

With data that implements Unpin, we can even move it out from the wrapper safe and sound:

use owned_pin::{opin, unpin};

// Pins the value onto the stack.
let pinned = opin!(String::from("Hello!"));
// Retrieves back the data because `String` is `Unpin`.
let string: String = unpin(pinned);


MIT OR Apache-2.0


~19K SLoC