Created
July 30, 2024 20:36
-
-
Save storopoli/4cca28979323306d20a9f478113a5ab5 to your computer and use it in GitHub Desktop.
Getting mutable references to a pinned struct in the stack
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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