fromsoftware_shared/
owned_pointer.rs

1use std::{
2    ops::{Deref, DerefMut},
3    ptr::NonNull,
4};
5
6/// Pointer to a structure that the containing structure owns. You will generally use this to model
7/// structures in foreign memory when extending the game libraries. Do not use this in your own
8/// code as you're risking all rusts safety reasoning.
9///
10/// # Safety
11///
12/// User must ensure that it's safe for this pointer to be turned into a (potentially mutable)
13/// reference if a reference to its embedding structure is obtained.
14#[repr(C)]
15pub struct OwnedPtr<T>(NonNull<T>);
16
17impl<T> OwnedPtr<T> {
18    pub fn as_ptr(&self) -> *mut T {
19        self.0.as_ptr()
20    }
21}
22
23impl<T> Deref for OwnedPtr<T> {
24    type Target = T;
25
26    fn deref(&self) -> &Self::Target {
27        unsafe { self.0.as_ref() }
28    }
29}
30
31impl<T> AsRef<T> for OwnedPtr<T> {
32    fn as_ref(&self) -> &T {
33        self.deref()
34    }
35}
36
37impl<T> DerefMut for OwnedPtr<T> {
38    fn deref_mut(&mut self) -> &mut Self::Target {
39        unsafe { self.0.as_mut() }
40    }
41}
42
43impl<T> AsMut<T> for OwnedPtr<T> {
44    fn as_mut(&mut self) -> &mut T {
45        self.deref_mut()
46    }
47}
48
49unsafe impl<T> Send for OwnedPtr<T> {}
50unsafe impl<T> Sync for OwnedPtr<T> where T: Sync {}