如何从可变 Pin 中获取不可变 Pin?

问题描述 投票:0回答:1

如何对包含在

&mut
内的引用应用从
&
Pin<>
的 deref 强制转换?即如何借
Pin<&mut _>
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);
rust reference coercion type-coercion rust-pin
1个回答
0
投票

Pin::as_ref()
是你的朋友:

take_immutable(mutable_ref.as_ref());
© www.soinside.com 2019 - 2024. All rights reserved.