Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How get an immutable Pin from a mutable Pin?

How to apply deref coercion from &mut to & for references that are wrapped inside Pin<>? That is, how to borrow Pin<&mut _> as Pin<&_>?

use std::pin::{Pin, pin};

fn take_immutable(_: Pin<&u32>) {}

let mutable_ref = pin!(0u32);
    
// Error: expected struct `Pin<&_>` 
//           found struct `Pin<&mut _>`
take_immutable(mutable_ref);
like image 400
tolvanea Avatar asked Mar 21 '26 03:03

tolvanea


1 Answers

Pin::as_ref() is your friend:

take_immutable(mutable_ref.as_ref());
like image 124
Chayim Friedman Avatar answered Mar 22 '26 20:03

Chayim Friedman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!