1 use std::ops::{Deref, DerefMut};
2 use std::convert::AsRef;
3 use std::sync::atomic::*;
5 struct NABufferData<T> {
10 impl<T> NABufferData<T> {
11 fn new(data: T) -> Self {
14 refs: AtomicUsize::new(1),
17 fn inc_refs(obj: &mut Self) {
18 obj.refs.fetch_add(1, Ordering::SeqCst);
20 fn dec_refs(obj: &mut Self) -> bool {
21 obj.refs.fetch_sub(1, Ordering::SeqCst) == 1
23 fn get_num_refs(obj: &Self) -> usize {
24 obj.refs.load(Ordering::Relaxed)
26 fn get_read_ptr(obj: &Self) -> &T {
29 fn get_write_ptr(obj: &mut Self) -> Option<&mut T> {
34 pub struct NABufferRef<T> {
35 ptr: *mut NABufferData<T>,
38 unsafe impl<T> Sync for NABufferRef<T> {}
39 unsafe impl<T> Send for NABufferRef<T> {}
41 impl<T> NABufferRef<T> {
42 pub fn new(val: T) -> Self {
43 let bdata = NABufferData::new(val);
44 let nbox: Box<_> = Box::new(bdata);
45 Self { ptr: Box::into_raw(nbox) }
47 pub fn get_num_refs(&self) -> usize {
49 NABufferData::get_num_refs(self.ptr.as_mut().unwrap())
52 pub fn as_mut(&mut self) -> Option<&mut T> {
54 NABufferData::get_write_ptr(self.ptr.as_mut().unwrap())
59 impl<T> AsRef<T> for NABufferRef<T> {
60 fn as_ref(&self) -> &T {
62 NABufferData::get_read_ptr(self.ptr.as_mut().unwrap())
67 impl<T> Deref for NABufferRef<T> {
69 fn deref(&self) -> &T { self.as_ref() }
72 impl<T> DerefMut for NABufferRef<T> {
73 fn deref_mut(&mut self) -> &mut T { self.as_mut().unwrap() }
76 impl<T> Clone for NABufferRef<T> {
77 fn clone(&self) -> Self {
79 NABufferData::inc_refs(self.ptr.as_mut().unwrap());
81 Self { ptr: self.ptr }
85 impl<T> Drop for NABufferRef<T> {
88 if NABufferData::dec_refs(self.ptr.as_mut().unwrap()) {
89 let data = Box::from_raw(self.ptr);
96 impl<T:Default> Default for NABufferRef<T> {
97 fn default() -> Self {
98 Self::new(T::default())