Pinning is a concept in the Rust programming language that requires a piece of data to never be moved from its location in memory. Questions with this tag are about the complexities of the pinning contract, transitioning between pinned references and mutable references, or calling methods on pinned objects. Pinning is closely tied to async in Rust; this tag is not for async-related questions unless the question specifically includes the pinning contract.
Questions tagged [rust-pin]
17 questions
23
votes
2 answers
What are the use cases of the newly proposed Pin type?
There is a new Pin type in unstable Rust and the RFC is already merged. It is said to be kind of a game changer when it comes to passing references, but I am not sure how and when one should use it.
Can anyone explain it in layman's terms?

00imvj00
- 665
- 7
- 18
9
votes
1 answer
How to use the Pin struct with self-referential structures?
I'm trying to use the new Pin feature. After reading this blog post, I've started to write some code:
#![feature(pin, arbitrary_self_types)]
use std::mem::Pin;
pub struct Foo {
var: i32,
}
pub struct FooRef<'i> {
ref_var: &'i i32,
…

Sébastien Chapuis
- 175
- 2
- 10
6
votes
1 answer
Pinned reference to FFI object consumed by calling methods that receive it
I'm writing a foreign function interface (ffi) to expose the API of a pre-existing C++ library to some new Rust code I am writing. I am using the Rust cxx module for this.
I am running into some problems related to Pin (a topic which I have to admit…

harmic
- 28,606
- 5
- 67
- 91
6
votes
1 answer
Should I use Pin when making C++ call a Rust method through a pointer?
I have C++ code that calls Rust code with data. It knows which object to send the data to. Here's an example of the Rust function that C++ calls back:
extern "C" fn on_open_vpn_receive(
instance: Box,
data: *mut c_uchar,
size:…

Guerlando OCs
- 1,886
- 9
- 61
- 150
3
votes
0 answers
How do I call a method that requires a pinned self from another method that has a pinned self?
I am implementing Tokio's AsyncRead for a struct, but the implementation is just a pass through to a contained struct. The implementation that I finally got to compile is:
use std::pin::Pin;
// A simplified version of Tokio's AsyncRead
trait Async…

WillW
- 871
- 6
- 18
2
votes
0 answers
Why does Pin prevent mem::swap? Does swapping change the memory location of objects?
I feel confused reading the Pin chapter in Futures Explained in 200 Lines of Rust.
I understand that moving a value breaks self-referential structs:
— pin. In Rust, pin is a property that…
The memory location of the struct changed and the self…

fairjm
- 1,115
- 12
- 26
2
votes
0 answers
How to manually implement a pinned, self-referential struct?
I'm trying to create the following design for a pinned, self-referential struct:
use std::future::Future;
use std::marker::PhantomPinned;
use std::pin::Pin;
struct Data {}
struct DataResult {}
async fn my_async(data: &Data) -> DataResult {
…

Lucretiel
- 3,145
- 1
- 24
- 52
2
votes
1 answer
How to get Pin<&mut T> out of Pin>>
I'm writing a stream adapter that requires a shared ownership of the stream.
Is it correct to store the stream (privately) in Pin>>?
How can I call poll_next(self: Pin<&mut Stream> on the stream?

Kornel
- 97,764
- 37
- 219
- 309
1
vote
1 answer
Why does inner variable borrow outer variable for longer than the inner variable is in scope?
I had the idea to use a pinus::sync::PineMap (GitHub) to make all references of equivalent objects actually reference the same object in memory (the "one" object would be a PineMap-owned value). I'm trying out PineMap for this because its insert…

theonlygusti
- 11,032
- 11
- 64
- 119
1
vote
1 answer
Why can't I use PineMap to store a referential cons-list? Getting "still borrowed" error even after end of main
Why does this code:
#[derive(Eq, PartialEq, Ord, PartialOrd)]
enum List<'a> {
Cons(isize, &'a List<'a>),
Nil,
}
fn main() {
use List::*;
use pinus::{prelude::*, sync::PineMap};
let table = PineMap::new();
table.insert(Nil,…

theonlygusti
- 11,032
- 11
- 64
- 119
1
vote
1 answer
Why Pin::new_unchecked(&self) worked but not Pin::new_unchecked(&self).as_ref()?
I was writing a StackBox that references a variable allocated on a memory-mapped stack and implemented Drop trait so that StackBox can be used as a reference.
Since I can guarantee that if StackBox is ever created by my code, then the memory-mapped…

JiaHao Xu
- 2,452
- 16
- 31
1
vote
1 answer
How is `pin_project` sound without `Unpin`
pin_project allows struct designers to get pinned references to struct fields out of a pinned reference to the whole struct. It further allows the designer to specify which fields need pinned references and which can simply get a regular…

Lucretiel
- 3,145
- 1
- 24
- 52
0
votes
1 answer
Actually pinning to the heap using alloc/dealloc in Rust?
This is something of a follow-up to a previous question of mine. TL;DR: I tried to create a self-referencing struct by heap allocating the self-referencee using a Box. It was pointed out that I can't rely on the pointer provenance not changing (it…

Einliterflasche
- 473
- 6
- 18
0
votes
1 answer
Why doesn't my self-referential struct work (invalid memory reference)?
I am trying to implement a self-referential struct in rust.
Background
I am writing a crate which is based on another crate. The underlying crate exposes a type Transaction<'a> which requires a mutable reference to a Client object:
struct…

Einliterflasche
- 473
- 6
- 18
0
votes
1 answer
Why does a pinned future implement Unpin?
fn test(t: T) {
}
fn main() {
let a = 1;
let b = async {
1
};
test(b) // the trait `Unpin` is not implemented for `[async block]`
}
Future is not Unpin, so why does pinning it (pin_mut, Box::pin, etc., which wrap…

BugMaker
- 111
- 1
- 3