{_unsafecell">
赞
踩
UnsafeCell是构建内部可变性Cell、RefCell、Mutex和RwLock的基础。
UnsafeCell在文档中被定义为Rust内部可变性的核心原语。
#[lang = "unsafe_cell"]
#[stable(feature = "rust1", since = "1.0.0")]
#[repr(transparent)]
#[repr(no_niche)] // rust-lang/rust#68303.
pub struct UnsafeCell<T: ?Sized> {
value: T,
}
看一下这个get()函数,它接收不可变引用,然后将其转换三次。先是原始常量指针 (*const T),然后还是一个原始常量指针,最后是一个原始可变指针 (*mut T),返回给调用者。
#[inline(always)]
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_stable(feature = "const_unsafecell_get", since = "1.32.0")]
pub const fn get(&self) -> *mut T {
// We can just cast the pointer from `UnsafeCell<T>` to `T` because of
// #[repr(transparent)]. This exploits libstd's special status, there is
// no guarantee for user code that this will work in future versions of the compiler!
self as *const UnsafeCell<T> as *const T as *mut T
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。