Skip to main content

ArrayProtocol

Trait ArrayProtocol 

Source
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§

Source

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 called
  • types - The types of all arguments that implement ArrayProtocol
  • args - The arguments to the function
  • kwargs - 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.

Source

fn as_any(&self) -> &dyn Any

Get the array as Any for downcasting

Source

fn box_clone(&self) -> Box<dyn ArrayProtocol>

Clone this array protocol object.

Provided Methods§

Source

fn shape(&self) -> &[usize]

Get the shape of the array (default implementation returns empty slice)

Source

fn dtype(&self) -> TypeId

Get the data type of the array (default implementation returns f64)

Source

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§

Source§

impl Clone for Box<dyn ArrayProtocol>

Make Box<dyn ArrayProtocol> cloneable via box_clone

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

Source§

impl ArrayProtocol for GpuNdarray<f32>

Source§

impl<T, A> ArrayProtocol for JITEnabledArray<T, A>
where T: Send + Sync + 'static, A: ArrayProtocol + Clone + Send + Sync + 'static,

Source§

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>,

Source§

impl<T, D> ArrayProtocol for DistributedNdarray<T, D>
where T: Clone + Send + Sync + 'static + Zero + Div<f64, Output = T> + Default + Add<Output = T> + Mul<Output = T>, D: Dimension + Clone + Send + Sync + 'static + RemoveAxis,

Source§

impl<T, D> ArrayProtocol for GPUNdarray<T, D>
where T: Clone + Send + Sync + 'static + Zero + Div<f64, Output = T> + Mul<Output = T> + Add<Output = T>, D: Dimension + Clone + Send + Sync + 'static + RemoveAxis,

Source§

impl<T, D> ArrayProtocol for MixedPrecisionArray<T, D>
where T: Clone + Float + Send + Sync + 'static, D: Dimension + Send + Sync + 'static,

Implement ArrayProtocol for MixedPrecisionArray.

Source§

impl<T, D> ArrayProtocol for NdarrayWrapper<T, D>
where T: Clone + Send + Sync + 'static, D: Dimension + Send + Sync + 'static,

Source§

impl<T: Clone + Send + Sync + 'static> ArrayProtocol for MockDistributedArray<T>

Source§

impl<T: Clone + Send + Sync + 'static> ArrayProtocol for MockGPUArray<T>