当前位置: 首页>>编程示例 >>用法及示例精选 >>正文


Rust Pin.as_mut用法及代码示例

本文简要介绍rust语言中 core::pin::Pin.as_mut 的用法。

用法

pub fn as_mut(&mut self) -> Pin<&mut P::Target>

从此固定指针获取固定的可变引用。

这是从 &mut Pin<Pointer<T>>Pin<&mut T> 的通用方法。这是安全的,因为作为 Pin::new_unchecked 合同的一部分,指针对象在 Pin<Pointer<T>> 创建后不能移动。 Pointer::DerefMut 的 “Malicious” 实现同样被 Pin::new_unchecked 的合同排除在外。

在对使用固定类型的函数进行多次调用时,此方法很有用。

示例

use std::pin::Pin;

impl Type {
    fn method(self: Pin<&mut Self>) {
        // do something
    }

    fn call_method_twice(mut self: Pin<&mut Self>) {
        // `method` consumes `self`, so reborrow the `Pin<&mut Self>` via `as_mut`.
        self.as_mut().method();
        self.as_mut().method();
    }
}

相关用法


注:本文由纯净天空筛选整理自rust-lang.org大神的英文原创作品 core::pin::Pin.as_mut。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。