use std::{collections::VecDeque, ops::{Deref, DerefMut}};
use crate::{context::{Global, Context}};
use super::{Svm};
pub unsafe trait SvmPointer<T: ?Sized> {
type Context: Context;
fn allocator (&self) -> &Svm<Self::Context>;
fn as_ptr (&self) -> *const T;
fn as_mut_ptr (&mut self) -> *mut T;
fn len (&self) -> usize;
}
pub type SvmBox<T, C = Global> = Box<T, Svm<C>>;
pub type SvmVec<T, C = Global> = Vec<T, Svm<C>>;
pub type SvmVecDeque<T, C = Global> = VecDeque<T, Svm<C>>;
unsafe impl<T: ?Sized, C: Context> SvmPointer<T> for SvmBox<T, C> {
type Context = C;
#[inline(always)]
fn allocator (&self) -> &Svm<C> {
Box::allocator(self)
}
#[inline(always)]
fn as_ptr (&self) -> *const T {
self.deref()
}
#[inline(always)]
fn as_mut_ptr (&mut self) -> *mut T {
self.deref_mut()
}
#[inline(always)]
fn len (&self) -> usize {
1
}
}
unsafe impl<T, C: Context> SvmPointer<T> for SvmBox<[T], C> {
type Context = C;
#[inline(always)]
fn allocator (&self) -> &Svm<C> {
Box::allocator(self)
}
#[inline(always)]
fn as_ptr (&self) -> *const T {
<[T]>::as_ptr(self)
}
#[inline(always)]
fn as_mut_ptr (&mut self) -> *mut T {
<[T]>::as_mut_ptr(self)
}
#[inline(always)]
fn len (&self) -> usize {
<[T]>::len(self)
}
}
unsafe impl<T, C: Context> SvmPointer<T> for SvmVec<T, C> {
type Context = C;
#[inline(always)]
fn allocator (&self) -> &Svm<C> {
Vec::allocator(self)
}
#[inline(always)]
fn as_ptr (&self) -> *const T {
Vec::as_ptr(self)
}
#[inline(always)]
fn as_mut_ptr (&mut self) -> *mut T {
Vec::as_mut_ptr(self)
}
#[inline(always)]
fn len (&self) -> usize {
Vec::len(self)
}
}
unsafe impl<T, C: Context> SvmPointer<[T]> for SvmVec<T, C> {
type Context = C;
#[inline(always)]
fn allocator (&self) -> &Svm<C> {
Vec::allocator(self)
}
#[inline(always)]
fn as_ptr (&self) -> *const [T] {
unsafe {
core::slice::from_raw_parts(Vec::as_ptr(self), self.len())
}
}
#[inline(always)]
fn as_mut_ptr (&mut self) -> *mut [T] {
unsafe {
core::slice::from_raw_parts_mut(Vec::as_mut_ptr(self), self.len())
}
}
#[inline(always)]
fn len (&self) -> usize {
1
}
}