Struct MLMultiArray

Source
#[repr(C)]
pub struct MLMultiArray { /* private fields */ }
Available on crate feature MLMultiArray only.
Expand description

Use MLMultiArray to store a multi-dimensional value.

Unlike MLShapedArray or MLTensor, MLMultiArray can be used in Obj-C code. Unlike MLTensor, MLMultiArray is always backed by a concrete storage.

The object has properties to define the interpretation of the storage.

.dataType defines the interpretation of raw bytes into a numeric scalar value. For example, MLMultiArrayDataTypeFloat32 means the backing storage uses IEEE 754 Float32 encoding.

.shape defines the multi-dimensional space. For example, 30 x 20 image with three color components (Red, Green, Blue) could be defined using the shape [3, 20, 30].

.strides defines the offset addressing of the scalar for a given coordinates. For example, the image above might use [640, 32, 1] as the strides. Then, the scalar at (1, 10, 15) is stored at 640 * 1 + 32 * 10 + 1 * 15, or 975th scalar in the storage. In general, the scalar offset for coordinates index and strides strides is:

scalarOffset = sum_d index[d]*strides[d]

The backing storage can be a heap allocated buffer or CVPixelBuffer. Though CVPixelBuffer backing supports limited data types, MLModel could share the storage with backend hardware such as Apple Neural Engine without copy.

See also Apple’s documentation

Implementations§

Source§

impl MLMultiArray

Source

pub unsafe fn dataPointer(&self) -> NonNull<c_void>

👎Deprecated: Use getBytesWithHandler or getMutableBytesWithHandler instead. For Swift, use withUnsafeBytes or withUnsafeMutableBytes.

Unsafe pointer to underlying buffer holding the data

Source

pub unsafe fn dataType(&self) -> MLMultiArrayDataType

Scalar’s data type.

Source

pub unsafe fn shape(&self) -> Retained<NSArray<NSNumber>>

Shape of the multi-dimensional space that this instance represents.

Source

pub unsafe fn strides(&self) -> Retained<NSArray<NSNumber>>

Strides.

It defines the offset of the scalar of a given coordinate index in the storage, which is:

scalarOffset = sum_d index[d]*strides[d]
Source

pub unsafe fn count(&self) -> NSInteger

Count of total number of addressable scalars.

The value is same as product_d shape[d].

Source

pub unsafe fn pixelBuffer(&self) -> Option<Retained<CVPixelBuffer>>

Available on crate feature objc2-core-video only.

Returns the backing pixel buffer if exists, otherwise nil.

Source§

impl MLMultiArray

Methods declared on superclass NSObject.

Source

pub unsafe fn init(this: Allocated<Self>) -> Retained<Self>

Source

pub unsafe fn new() -> Retained<Self>

Source§

impl MLMultiArray

Creation.

Source

pub unsafe fn initWithShape_dataType_error( this: Allocated<Self>, shape: &NSArray<NSNumber>, data_type: MLMultiArrayDataType, ) -> Result<Retained<Self>, Retained<NSError>>

Creates the object.

The contents of the object are left uninitialized; the client must initialize it.

The scalars will use the first-major contiguous layout.

  • Parameters:
  • shape: The shape
  • dataType: The data type
  • error: Filled with error information on error.
Source

pub unsafe fn initWithShape_dataType_strides( this: Allocated<Self>, shape: &NSArray<NSNumber>, data_type: MLMultiArrayDataType, strides: &NSArray<NSNumber>, ) -> Retained<Self>

Creates the object with specified strides.

The contents of the object are left uninitialized; the client must initialize it.

let shape = [2, 3];
let strides = [4, 1]

let multiArray = MLMultiArray(shape: shape, dataType: .float32, strides: strides)
XCTAssertEqual(multiArray.shape, shape as [NSNumber])
XCTAssertEqual(multiArray.strides, strides as [NSNumber])
NSArray
<NSNumber
*> *shape =
@
[
@
2,
@
3];
NSArray
<NSNumber
*> *strides =
@
[
@
4,
@
1];

MLMultiArray *multiArray = [[MLMultiArray alloc] initWithShape:shape
dataType:MLMultiArrayDataTypeFloat32
strides:strides];
XCTAssertEqualObjects(multiArray.shape, shape);
XCTAssertEqualObjects(multiArray.strides, strides);
  • Parameters:
  • shape: The shape
  • dataType: The data type
  • strides: The strides.
Source

pub unsafe fn initWithDataPointer_shape_dataType_strides_deallocator_error( this: Allocated<Self>, data_pointer: NonNull<c_void>, shape: &NSArray<NSNumber>, data_type: MLMultiArrayDataType, strides: &NSArray<NSNumber>, deallocator: Option<&DynBlock<dyn Fn(NonNull<c_void>)>>, ) -> Result<Retained<Self>, Retained<NSError>>

Available on crate feature block2 only.

Creates the object with existing data without copy.

Use this initializer to reference the existing buffer as the storage without copy.

int32_t *buffer = malloc(sizeof(int32_t) * 2 * 3 * 4);
MLMultiArray *multiArray = [[MLMultiArray alloc] initWithDataPointer:buffer
shape:
@
[
@
2,
@
3,
@
4]
dataType:MLMultiArrayDataTypeInt32
strides:
@
[
@
12,
@
4,
@
1]
deallocator:^(void *bytes) { free(bytes); }
error:NULL];
  • Parameters:
  • dataPointer: The pointer to the buffer.
  • shape: The shape
  • dataType: The data type
  • strides: The strides.
  • deallocator: Block to be called on the deallocation of the instance.
  • error: Filled with error information on error.
Source

pub unsafe fn initWithPixelBuffer_shape( this: Allocated<Self>, pixel_buffer: &CVPixelBuffer, shape: &NSArray<NSNumber>, ) -> Retained<Self>

Available on crate feature objc2-core-video only.

Create by wrapping a pixel buffer.

Use this initializer to create an IOSurface backed MLMultiArray, which can reduce the inference latency by avoiding the buffer copy.

The instance will own the pixel buffer and release it on the deallocation.

The pixel buffer’s pixel format type must be OneComponent16Half. As such, MLMultiArray’s data type will be MLMultiArrayDataTypeFloat16.

CVPixelBufferRef pixelBuffer = NULL;
NSDictionary* pixelBufferAttributes =
@
{
(id)kCVPixelBufferIOSurfacePropertiesKey:
@
{}
};

// Since shape == [2, 3, 4], width is 4 (= shape[2]) and height is 6 (= shape[0] * shape[1]).
CVPixelBufferCreate(kCFAllocatorDefault, 4, 6, kCVPixelFormatType_OneComponent16Half, (__bridge CFDictionaryRef)pixelBufferAttributes,
&pixelBuffer
);
MLMultiArray *multiArray = [[MLMultiArray alloc] initWithPixelBuffer:pixelBuffer shape:
@
[
@
2,
@
3,
@
4]];
  • Parameters:
  • pixelBuffer: The pixel buffer to be owned by the instance.
  • shape: The shape of the MLMultiArray. The last dimension of shape must match the pixel buffer’s width. The product of the rest of the dimensions must match the height.
Source§

impl MLMultiArray

ScopedBufferAccess.

Source

pub unsafe fn getBytesWithHandler( &self, handler: &DynBlock<dyn Fn(NonNull<c_void>, NSInteger) + '_>, )

Available on crate feature block2 only.

Get the underlying buffer pointer to read.

The buffer pointer is valid only within the block.

MLMultiArray * A = [[MLMultiArray alloc] initWithShape:
@
[
@
3,
@
2] dataType:MLMultiArrayDataTypeInt32 error:NULL];
A[
@
[
@
1,
@
2]] =
@
42;
[A getBytesWithHandler:^(const void *bytes, NSInteger size) {
const int32_t *scalarBuffer = (const int32_t *)bytes;
const int strideY = A.strides[0].intValue;
// Print 42
NSLog(
"
Scalar at (1, 2): %d", scalarBuffer[1 * strideY + 2]);
}];
  • Parameters:
  • handler: The block to receive the buffer pointer and its size in bytes.
Source

pub unsafe fn getMutableBytesWithHandler( &self, handler: &DynBlock<dyn Fn(NonNull<c_void>, NSInteger, NonNull<NSArray<NSNumber>>) + '_>, )

Available on crate feature block2 only.

Get the underlying buffer pointer to mutate.

The buffer pointer is valid only within the block.

Use strides parameter passed in the block because the method may switch to a new backing buffer with different strides.

MLMultiArray * A = [[MLMultiArray alloc] initWithShape:
@
[
@
3,
@
2] dataType:MLMultiArrayDataTypeInt32 error:NULL];
[A getMutableBytesWithHandler:^(void *bytes, NSInteger __unused size, NSArray
<NSNumber
*> *strides) {
int32_t *scalarBuffer = (int32_t *)bytes;
const int strideY = strides[0].intValue;
scalarBuffer[1 * strideY + 2] = 42;  // Set 42 at A[1, 2]
}];
  • Parameters:
  • handler: The block to receive the buffer pointer, size in bytes, and strides.
Source§

impl MLMultiArray

Concatenating.

Source

pub unsafe fn multiArrayByConcatenatingMultiArrays_alongAxis_dataType( multi_arrays: &NSArray<MLMultiArray>, axis: NSInteger, data_type: MLMultiArrayDataType, ) -> Retained<Self>

Concatenate MLMultiArrays to form a new MLMultiArray.

All the source MLMultiArrays must have a same shape except the specified axis. The resultant MLMultiArray has the same shape as inputs except this axis, which dimension will be the sum of all the input dimensions of the axis.

For example,

// Swift
let A = try MLMultiArray(shape: [2, 3], dataType: .int32)
let B = try MLMultiArray(shape: [2, 2], dataType: .int32)
let C = MLMultiArray(concatenating: [A, B], axis: 1, dataType: .int32)
assert(C.shape == [2, 5])
// Obj-C
MLMultiArray *A = [[MLMultiArray alloc] initWithShape:
@
[
@
2,
@
3] dataType:MLMultiArrayDataTypeInt32 error:NULL];
MLMultiArray *B = [[MLMultiArray alloc] initWithShape:
@
[
@
2,
@
2] dataType:MLMultiArrayDataTypeInt32 error:NULL];
MLMultiArray *C = [MLMultiArray multiArrayByConcatenatingMultiArrays:
@
[A, B] alongAxis:1 dataType:MLMultiArrayDataTypeInt32];
assert(C.shape ==
@
[
@
2,
@
5])

Numeric data will be up or down casted as needed.

The method raises NSInvalidArgumentException if the shapes of input multi arrays are not compatible for concatenation.

  • Parameters:
  • multiArrays: Array of MLMultiArray instances to be concatenated.
  • axis: Axis index with which the concatenation will performed. The value is wrapped by the dimension of the axis. For example, -1 is the last axis.
  • dataType: The data type of the resultant MLMultiArray.
Source§

impl MLMultiArray

NSNumberDataAccess.

Source

pub unsafe fn objectAtIndexedSubscript( &self, idx: NSInteger, ) -> Retained<NSNumber>

Get a value by its linear index (assumes C-style index ordering)

Source

pub unsafe fn objectForKeyedSubscript( &self, key: &NSArray<NSNumber>, ) -> Retained<NSNumber>

Get a value by its multidimensional index (NSArray <NSNumber *>)

Source

pub unsafe fn setObject_atIndexedSubscript( &self, obj: &NSNumber, idx: NSInteger, )

Set a value by its linear index (assumes C-style index ordering)

Source

pub unsafe fn setObject_forKeyedSubscript( &self, obj: &NSNumber, key: &NSArray<NSNumber>, )

Set a value by subindicies (NSArray <NSNumber *>)

Source§

impl MLMultiArray

Transferring.

Source

pub unsafe fn transferToMultiArray( &self, destination_multi_array: &MLMultiArray, )

Transfer the contents to the destination multi-array.

Numeric data will be up or down casted as needed. It can transfer to a multi-array with different layout (strides).

let sourceMultiArray: MLMultiArray = ... // shape is [2, 3] and data type is Float64

let newStrides = [4, 1]
let destinationMultiArray = MLMultiArray(shape: [2, 3],
dataType: .float32,
strides: newStrides)
sourceMultiArray.transfer(to: destinationMultiArray)
NSArray
<NSNumber
*> *shape =
@
[
@
2,
@
3];
NSArray
<NSNumber
*> *sourceStrides =
@
[
@
3,
@
1];
NSArray
<NSNumber
*> *destinationStrides =
@
[
@
4,
@
1];
MLMultiArray *source = [[MLMultiArray alloc] initWithShape:shape
dataType:MLMultiArrayDataTypeDouble
strides:sourceStrides];
// Initialize source...

MLMultiArray *destination = [[MLMultiArray alloc] initWithShape:shape
dataType:MLMultiArrayDataTypeFloat32
strides:destinationStrides];
[source transferToMultiArray:destination];
  • Parameters:
  • destinationMultiArray: The transfer destination.

Methods from Deref<Target = NSObject>§

Source

pub fn doesNotRecognizeSelector(&self, sel: Sel) -> !

Handle messages the object doesn’t recognize.

See Apple’s documentation for details.

Methods from Deref<Target = AnyObject>§

Source

pub fn class(&self) -> &'static AnyClass

Dynamically find the class of this object.

§Panics

May panic if the object is invalid (which may be the case for objects returned from unavailable init/new methods).

§Example

Check that an instance of NSObject has the precise class NSObject.

use objc2::ClassType;
use objc2::runtime::NSObject;

let obj = NSObject::new();
assert_eq!(obj.class(), NSObject::class());
Source

pub unsafe fn get_ivar<T>(&self, name: &str) -> &T
where T: Encode,

👎Deprecated: this is difficult to use correctly, use Ivar::load instead.

Use Ivar::load instead.

§Safety

The object must have an instance variable with the given name, and it must be of type T.

See Ivar::load_ptr for details surrounding this.

Source

pub fn downcast_ref<T>(&self) -> Option<&T>
where T: DowncastTarget,

Attempt to downcast the object to a class of type T.

This is the reference-variant. Use Retained::downcast if you want to convert a retained object to another type.

§Mutable classes

Some classes have immutable and mutable variants, such as NSString and NSMutableString.

When some Objective-C API signature says it gives you an immutable class, it generally expects you to not mutate that, even though it may technically be mutable “under the hood”.

So using this method to convert a NSString to a NSMutableString, while not unsound, is generally frowned upon unless you created the string yourself, or the API explicitly documents the string to be mutable.

See Apple’s documentation on mutability and on isKindOfClass: for more details.

§Generic classes

Objective-C generics are called “lightweight generics”, and that’s because they aren’t exposed in the runtime. This makes it impossible to safely downcast to generic collections, so this is disallowed by this method.

You can, however, safely downcast to generic collections where all the type-parameters are AnyObject.

§Panics

This works internally by calling isKindOfClass:. That means that the object must have the instance method of that name, and an exception will be thrown (if CoreFoundation is linked) or the process will abort if that is not the case. In the vast majority of cases, you don’t need to worry about this, since both root objects NSObject and NSProxy implement this method.

§Examples

Cast an NSString back and forth from NSObject.

use objc2::rc::Retained;
use objc2_foundation::{NSObject, NSString};

let obj: Retained<NSObject> = NSString::new().into_super();
let string = obj.downcast_ref::<NSString>().unwrap();
// Or with `downcast`, if we do not need the object afterwards
let string = obj.downcast::<NSString>().unwrap();

Try (and fail) to cast an NSObject to an NSString.

use objc2_foundation::{NSObject, NSString};

let obj = NSObject::new();
assert!(obj.downcast_ref::<NSString>().is_none());

Try to cast to an array of strings.

use objc2_foundation::{NSArray, NSObject, NSString};

let arr = NSArray::from_retained_slice(&[NSObject::new()]);
// This is invalid and doesn't type check.
let arr = arr.downcast_ref::<NSArray<NSString>>();

This fails to compile, since it would require enumerating over the array to ensure that each element is of the desired type, which is a performance pitfall.

Downcast when processing each element instead.

use objc2_foundation::{NSArray, NSObject, NSString};

let arr = NSArray::from_retained_slice(&[NSObject::new()]);

for elem in arr {
    if let Some(data) = elem.downcast_ref::<NSString>() {
        // handle `data`
    }
}

Trait Implementations§

Source§

impl AsRef<AnyObject> for MLMultiArray

Source§

fn as_ref(&self) -> &AnyObject

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl AsRef<MLMultiArray> for MLMultiArray

Source§

fn as_ref(&self) -> &Self

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl AsRef<NSObject> for MLMultiArray

Source§

fn as_ref(&self) -> &NSObject

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl Borrow<AnyObject> for MLMultiArray

Source§

fn borrow(&self) -> &AnyObject

Immutably borrows from an owned value. Read more
Source§

impl Borrow<NSObject> for MLMultiArray

Source§

fn borrow(&self) -> &NSObject

Immutably borrows from an owned value. Read more
Source§

impl ClassType for MLMultiArray

Source§

const NAME: &'static str = "MLMultiArray"

The name of the Objective-C class that this type represents. Read more
Source§

type Super = NSObject

The superclass of this class. Read more
Source§

type ThreadKind = <<MLMultiArray as ClassType>::Super as ClassType>::ThreadKind

Whether the type can be used from any thread, or from only the main thread. Read more
Source§

fn class() -> &'static AnyClass

Get a reference to the Objective-C class that this type represents. Read more
Source§

fn as_super(&self) -> &Self::Super

Get an immutable reference to the superclass.
Source§

impl Debug for MLMultiArray

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Deref for MLMultiArray

Source§

type Target = NSObject

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl Hash for MLMultiArray

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Message for MLMultiArray

Source§

fn retain(&self) -> Retained<Self>
where Self: Sized,

Increment the reference count of the receiver. Read more
Source§

impl NSObjectProtocol for MLMultiArray

Source§

fn isEqual(&self, other: Option<&AnyObject>) -> bool
where Self: Sized + Message,

Check whether the object is equal to an arbitrary other object. Read more
Source§

fn hash(&self) -> usize
where Self: Sized + Message,

An integer that can be used as a table address in a hash table structure. Read more
Source§

fn isKindOfClass(&self, cls: &AnyClass) -> bool
where Self: Sized + Message,

Check if the object is an instance of the class, or one of its subclasses. Read more
Source§

fn is_kind_of<T>(&self) -> bool
where T: ClassType, Self: Sized + Message,

👎Deprecated: use isKindOfClass directly, or cast your objects with AnyObject::downcast_ref
Check if the object is an instance of the class type, or one of its subclasses. Read more
Source§

fn isMemberOfClass(&self, cls: &AnyClass) -> bool
where Self: Sized + Message,

Check if the object is an instance of a specific class, without checking subclasses. Read more
Source§

fn respondsToSelector(&self, aSelector: Sel) -> bool
where Self: Sized + Message,

Check whether the object implements or inherits a method with the given selector. Read more
Source§

fn conformsToProtocol(&self, aProtocol: &AnyProtocol) -> bool
where Self: Sized + Message,

Check whether the object conforms to a given protocol. Read more
Source§

fn description(&self) -> Retained<NSObject>
where Self: Sized + Message,

A textual representation of the object. Read more
Source§

fn debugDescription(&self) -> Retained<NSObject>
where Self: Sized + Message,

A textual representation of the object to use when debugging. Read more
Source§

fn isProxy(&self) -> bool
where Self: Sized + Message,

Check whether the receiver is a subclass of the NSProxy root class instead of the usual NSObject. Read more
Source§

fn retainCount(&self) -> usize
where Self: Sized + Message,

The reference count of the object. Read more
Source§

impl NSSecureCoding for MLMultiArray

Source§

unsafe fn supportsSecureCoding() -> bool
where Self: Sized + ClassType,

Source§

impl PartialEq for MLMultiArray

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl RefEncode for MLMultiArray

Source§

const ENCODING_REF: Encoding = <NSObject as ::objc2::RefEncode>::ENCODING_REF

The Objective-C type-encoding for a reference of this type. Read more
Source§

impl DowncastTarget for MLMultiArray

Source§

impl Eq for MLMultiArray

Source§

impl NSCoding for MLMultiArray

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<'a, T> AnyThread for T
where T: ClassType<ThreadKind = dyn AnyThread + 'a> + ?Sized,

Source§

fn alloc() -> Allocated<Self>
where Self: Sized + ClassType,

Allocate a new instance of the class. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> AutoreleaseSafe for T
where T: ?Sized,