use {
std::{
ops::{
Deref
},
rc::Rc,
sync::Arc
},
crate::{
core::{
data_type::{
DataType,
Type,
as_byte_slice_mut
},
indices::{
ToIndices
},
shape::{
Shape
}
}
}
};
pub trait DataSource: Sized {
fn data_type( &self ) -> Type;
fn shape( &self ) -> Shape;
fn len( &self ) -> usize;
fn gather_bytes_into< I >( &self, indices: I, output: &mut [u8] ) where I: ToIndices;
fn gather_into< T, I >( &self, indices: I, output: &mut [T] ) where I: ToIndices, T: DataType {
assert_eq!( T::TYPE, self.data_type() );
self.gather_bytes_into( indices, as_byte_slice_mut( output ) );
}
fn is_empty( &self ) -> bool {
self.len() == 0
}
}
impl< 'r, S > DataSource for &'r S where S: DataSource {
fn data_type( &self ) -> Type {
DataSource::data_type( *self )
}
fn shape( &self ) -> Shape {
DataSource::shape( *self )
}
fn len( &self ) -> usize {
DataSource::len( *self )
}
fn gather_bytes_into< I >( &self, indices: I, output: &mut [u8] ) where I: ToIndices {
DataSource::gather_bytes_into( *self, indices, output )
}
}
impl< S > DataSource for Rc< S > where S: DataSource {
fn data_type( &self ) -> Type {
DataSource::data_type( self.deref() )
}
fn shape( &self ) -> Shape {
DataSource::shape( self.deref() )
}
fn len( &self ) -> usize {
DataSource::len( self.deref() )
}
fn gather_bytes_into< I >( &self, indices: I, output: &mut [u8] ) where I: ToIndices {
DataSource::gather_bytes_into( self.deref(), indices, output )
}
}
impl< S > DataSource for Arc< S > where S: DataSource {
fn data_type( &self ) -> Type {
DataSource::data_type( self.deref() )
}
fn shape( &self ) -> Shape {
DataSource::shape( self.deref() )
}
fn len( &self ) -> usize {
DataSource::len( self.deref() )
}
fn gather_bytes_into< I >( &self, indices: I, output: &mut [u8] ) where I: ToIndices {
DataSource::gather_bytes_into( self.deref(), indices, output )
}
}