本文簡要介紹rust語言中 alloc::sync::Weak.as_ptr
的用法。
用法
pub fn as_ptr(&self) -> *const T
返回指向此 Weak<T>
指向的對象 T
的原始指針。
隻有存在一些強引用時,指針才有效。指針可能懸空、未對齊或什至 null
否則。
例子
use std::sync::Arc;
use std::ptr;
let strong = Arc::new("hello".to_owned());
let weak = Arc::downgrade(&strong);
// Both point to the same object
assert!(ptr::eq(&*strong, weak.as_ptr()));
// The strong here keeps it alive, so we can still access the object.
assert_eq!("hello", unsafe { &*weak.as_ptr() });
drop(strong);
// But not any more. We can do weak.as_ptr(), but accessing the pointer would lead to
// undefined behaviour.
// assert_eq!("hello", unsafe { &*weak.as_ptr() });
相關用法
- Rust Weak.new用法及代碼示例
- Rust Weak.from_raw用法及代碼示例
- Rust Weak.into_raw用法及代碼示例
- Rust Weak.upgrade用法及代碼示例
- Rust Weak.ptr_eq用法及代碼示例
- Rust Wrapping.is_negative用法及代碼示例
- Rust Wrapping.from_le用法及代碼示例
- Rust Wrapping.reverse_bits用法及代碼示例
- Rust Wrapping.to_be用法及代碼示例
- Rust Wrapping.next_power_of_two用法及代碼示例
- Rust Write.write_all用法及代碼示例
- Rust Write.write_str用法及代碼示例
- Rust Windows用法及代碼示例
- Rust Write用法及代碼示例
- Rust Write.write_vectored用法及代碼示例
- Rust Wrapping.is_power_of_two用法及代碼示例
- Rust Wrapping.abs用法及代碼示例
- Rust WriterPanicked用法及代碼示例
- Rust Write.by_ref用法及代碼示例
- Rust Wrapping.rotate_right用法及代碼示例
- Rust Wake用法及代碼示例
- Rust Wrapping.leading_zeros用法及代碼示例
- Rust Write.flush用法及代碼示例
- Rust Wrapping.signum用法及代碼示例
- Rust Write.write_all_vectored用法及代碼示例
注:本文由純淨天空篩選整理自rust-lang.org大神的英文原創作品 alloc::sync::Weak.as_ptr。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。