pub trait ArrayProtocol:
Any
+ Send
+ Sync {
// Required methods
fn array_function(
&self,
func: &ArrayFunction,
types: &[TypeId],
args: &[Box<dyn Any>],
kwargs: &HashMap<String, Box<dyn Any>>,
) -> Result<Box<dyn Any>, NotImplemented>;
fn as_any(&self) -> &dyn Any;
fn box_clone(&self) -> Box<dyn ArrayProtocol>;
// Provided methods
fn shape(&self) -> &[usize] { ... }
fn dtype(&self) -> TypeId { ... }
fn supports_op(&self, _opname: &str) -> bool { ... }
}Expand description
Trait for objects that can handle the array protocol.
This is similar to NumPy’s __array_function__ protocol.
Required Methods§
Sourcefn array_function(
&self,
func: &ArrayFunction,
types: &[TypeId],
args: &[Box<dyn Any>],
kwargs: &HashMap<String, Box<dyn Any>>,
) -> Result<Box<dyn Any>, NotImplemented>
fn array_function( &self, func: &ArrayFunction, types: &[TypeId], args: &[Box<dyn Any>], kwargs: &HashMap<String, Box<dyn Any>>, ) -> Result<Box<dyn Any>, NotImplemented>
Implementation of the array protocol.
func- The function being calledtypes- The types of all arguments that implementArrayProtocolargs- The arguments to the functionkwargs- Named arguments to the function
Returns Ok(result) if the operation is successful, or Err(NotImplemented)
if the operation is not implemented for this type.
§Errors
Returns Err(NotImplemented) if the operation is not supported by this array type.
Sourcefn box_clone(&self) -> Box<dyn ArrayProtocol>
fn box_clone(&self) -> Box<dyn ArrayProtocol>
Clone this array protocol object.
Provided Methods§
Sourcefn shape(&self) -> &[usize]
fn shape(&self) -> &[usize]
Get the shape of the array (default implementation returns empty slice)
Sourcefn supports_op(&self, _opname: &str) -> bool
fn supports_op(&self, _opname: &str) -> bool
Whether this concrete array type provides its own array_function
handling for the named operation.
Dispatch uses this to decide whether it’s worth delegating to this
argument’s own array_function at all. Types that only implement a
subset of operations (like the plain NdarrayWrapper, whose
array_function only matches a handful of operation names) should
override this to report false for anything outside that subset, so
callers correctly fall through to another candidate (or to a
plain-ndarray fallback implementation) instead of receiving a
spurious NotImplemented from this type’s dispatch and giving up
immediately.
The default assumes the type may handle any operation:
array_function itself remains the ultimate authority (returning
Err(NotImplemented) when it truly doesn’t apply), so types that
already do that correctly (or that implement most/all operations)
don’t need to override this.
Trait Implementations§
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".
Implementors§
impl ArrayProtocol for GpuNdarray<f32>
impl<T, A> ArrayProtocol for JITEnabledArray<T, A>
impl<T, D> ArrayProtocol for AutoDevice<T, D>where
T: Clone + Send + Sync + 'static + Zero + Div<f64, Output = T> + Default + Mul<Output = T> + Add<Output = T>,
D: Dimension + RemoveAxis + 'static,
SliceInfo<[SliceInfoElem; 1], Dim<[usize; 1]>, Dim<[usize; 1]>>: SliceArg<D>,
SliceInfo<[SliceInfoElem; 2], Dim<[usize; 2]>, Dim<[usize; 2]>>: SliceArg<D>,
impl<T, D> ArrayProtocol for DistributedNdarray<T, D>
impl<T, D> ArrayProtocol for GPUNdarray<T, D>
impl<T, D> ArrayProtocol for MixedPrecisionArray<T, D>
Implement ArrayProtocol for MixedPrecisionArray.