Skip to content

Instantly share code, notes, and snippets.

@storopoli
Created July 30, 2024 20:36
Show Gist options
  • Save storopoli/4cca28979323306d20a9f478113a5ab5 to your computer and use it in GitHub Desktop.
Save storopoli/4cca28979323306d20a9f478113a5ab5 to your computer and use it in GitHub Desktop.
Getting mutable references to a pinned struct in the stack
use std::pin::pin;
use std::marker::PhantomPinned;
#[derive(Debug)]
struct MyStruct {
field: u32,
_pin: PhantomPinned
}
impl MyStruct {
fn new(field: u32) -> Self {
Self {
field,
_pin: PhantomPinned,
}
}
}
fn func(x: &mut MyStruct) {
x.field = 10;
}
fn main() {
let mut x = MyStruct::new(5);
let mut pinned_x = pin!(&mut x);
func(&mut pinned_x);
println!("{:?}", pinned_x)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment