1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
// Oliver Berzs
// https://github.com/oberzs/duku

use std::fmt;
use std::marker::PhantomData;
use std::ops::Deref;
use std::ops::DerefMut;

pub(crate) struct Resource<T> {
    pub(crate) value: T,
    pub(crate) count: u32,
    pub(crate) mutated: bool,
}

/// A handle to a rendering resource.
///
/// Gives access to the underlying resource
/// through the [Deref](std::ops::Deref) trait.
///
/// Note: cloning this does not create a new resource
pub struct Handle<T> {
    _marker: PhantomData<*const T>,
    value: *mut T,
    count: *mut u32,
    mutated: *mut bool,
}

impl<T> Resource<T> {
    pub(crate) const fn new(value: T) -> Self {
        Self {
            count: 0,
            mutated: false,
            value,
        }
    }

    pub(crate) fn handle(&mut self) -> Handle<T> {
        self.count += 1;

        Handle {
            _marker: PhantomData::default(),
            value: &mut self.value,
            count: &mut self.count,
            mutated: &mut self.mutated,
        }
    }
}

impl<T> Deref for Handle<T> {
    type Target = T;

    fn deref(&self) -> &T {
        unsafe { self.value.as_ref().expect("bad pointer") }
    }
}

impl<T> DerefMut for Handle<T> {
    fn deref_mut(&mut self) -> &mut T {
        unsafe {
            let mutated = self.mutated.as_mut().expect("bad pointer");
            *mutated = true;
            self.value.as_mut().expect("bad pointer")
        }
    }
}

impl<T> Clone for Handle<T> {
    fn clone(&self) -> Self {
        // increase count
        let count = unsafe { self.count.as_mut().expect("bad pointer") };
        *count += 1;

        Self {
            _marker: PhantomData::default(),
            value: self.value,
            count: self.count,
            mutated: self.mutated,
        }
    }
}

impl<T> Drop for Handle<T> {
    fn drop(&mut self) {
        // decrease count
        let count = unsafe { self.count.as_mut().expect("bad pointer") };

        // add checking if user holds on to handle
        // longer than duku context
        if let Some(c) = count.checked_sub(1) {
            *count = c;
        }
    }
}

impl<T> fmt::Debug for Handle<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Handle({:p})", self.value)
    }
}

impl<T> PartialEq for Handle<T> {
    fn eq(&self, other: &Self) -> bool {
        self.value == other.value
    }
}