pub struct Owned<T> { /* private fields */ }Expand description
Implementations§
Source§impl<T: 'static> Owned<T>
impl<T: 'static> Owned<T>
Sourcepub fn new(t: T) -> Self
pub fn new(t: T) -> Self
Creates a new instance of Owned.
The type of the instance must be determined at compile-time, must not contain non-static
references, and must not be a non-static reference since the instance can theoretically
survive the process. For instance, struct Disallowed<'l, T>(&'l T) is not allowed,
because an instance of the type cannot outlive 'l whereas the garbage collector does not
guarantee that the instance is dropped within 'l.
§Examples
use sdd::Owned;
let owned: Owned<usize> = Owned::new(31);Sourcepub fn new_with<F: FnOnce() -> T>(f: F) -> Owned<T>
pub fn new_with<F: FnOnce() -> T>(f: F) -> Owned<T>
Creates a new Owned with the provided function.
This function is identical to Owned::new except that the value is constructed after
memory allocation.
§Examples
use sdd::Owned;
let owned: Owned<String> = Owned::new_with(|| String::from("hello"));Source§impl<T> Owned<T>
impl<T> Owned<T>
Sourcepub unsafe fn new_unchecked(t: T) -> Self
pub unsafe fn new_unchecked(t: T) -> Self
Creates a new Owned without checking the lifetime of T.
§Safety
T::drop can be run after the Owned is dropped, therefore it is safe only if T::drop
does not access short-lived data or std::mem::needs_drop is false for T.
§Examples
use sdd::Owned;
let hello = String::from("hello");
let owned: Owned<&str> = unsafe { Owned::new_unchecked(hello.as_str()) };Sourcepub unsafe fn new_with_unchecked<F: FnOnce() -> T>(f: F) -> Owned<T>
pub unsafe fn new_with_unchecked<F: FnOnce() -> T>(f: F) -> Owned<T>
Creates a new Owned with the provided function without checking the lifetime of T.
This function is identical to Owned::new_unchecked except that the value is constructed
after memory allocation.
§Safety
See Owned::new_unchecked.
§Examples
use sdd::Owned;
let hello = String::from("hello");
let owned: Owned<&str> = unsafe { Owned::new_with_unchecked(|| hello.as_str()) };Sourcepub const fn get_guarded_ptr<'g>(&self, _guard: &'g Guard) -> Ptr<'g, T>
pub const fn get_guarded_ptr<'g>(&self, _guard: &'g Guard) -> Ptr<'g, T>
Sourcepub const fn get_guarded_ref<'g>(&self, _guard: &'g Guard) -> &'g T
pub const fn get_guarded_ref<'g>(&self, _guard: &'g Guard) -> &'g T
Sourcepub const fn as_ptr(&self) -> *const T
pub const fn as_ptr(&self) -> *const T
Provides a raw pointer to the instance.
§Examples
use sdd::Owned;
let owned: Owned<usize> = Owned::new(10);
assert_eq!(unsafe { *owned.as_ptr() }, 10);Sourcepub const fn as_non_null_ptr(&self) -> NonNull<T>
pub const fn as_non_null_ptr(&self) -> NonNull<T>
Provides a raw non-null pointer to the instance.
§Examples
use sdd::Owned;
let owned: Owned<usize> = Owned::new(10);
assert_eq!(unsafe { *owned.as_non_null_ptr().as_ref() }, 10);Sourcepub unsafe fn drop_in_place(self)
pub unsafe fn drop_in_place(self)
Drops the instance immediately.
§Safety
The caller must ensure that there is no Ptr pointing to the instance.
§Examples
use sdd::Owned;
use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering::Relaxed;
static DROPPED: AtomicBool = AtomicBool::new(false);
struct T(&'static AtomicBool);
impl Drop for T {
fn drop(&mut self) {
self.0.store(true, Relaxed);
}
}
let owned: Owned<T> = Owned::new(T(&DROPPED));
assert!(!DROPPED.load(Relaxed));
unsafe {
owned.drop_in_place();
}
assert!(DROPPED.load(Relaxed));