本文简要介绍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 Pin.new_unchecked用法及代码示例
- Rust PidFd用法及代码示例
- Rust PanicInfo.payload用法及代码示例
- Rust Path.components用法及代码示例
- Rust PathBuf.with_capacity用法及代码示例
- Rust Poll.map用法及代码示例
- Rust Peekable.peek用法及代码示例
- Rust Path.is_symlink用法及代码示例
- Rust Poll.map_ok用法及代码示例
- Rust Pointer用法及代码示例
- Rust Path.canonicalize用法及代码示例
- Rust PartialOrd.partial_cmp用法及代码示例
- Rust Path.is_relative用法及代码示例
- Rust Path.file_stem用法及代码示例
- Rust PermissionsExt.set_mode用法及代码示例
- Rust Path.to_string_lossy用法及代码示例
- Rust Path.display用法及代码示例
- Rust PathBuf.into_os_string用法及代码示例
- Rust Peekable.next_if用法及代码示例
- Rust PanicInfo用法及代码示例
- Rust PathBuf.pop用法及代码示例
- Rust Path.ancestors用法及代码示例
- Rust Path用法及代码示例
- Rust Path.is_dir用法及代码示例
- Rust Path.strip_prefix用法及代码示例
注:本文由纯净天空筛选整理自rust-lang.org大神的英文原创作品 core::pin::Pin.as_mut。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。