ShapeContract

Struct ShapeContract 

Source
pub struct ShapeContract<'a> {
    pub index: &'a [&'a str],
    pub terms: &'a [DimMatcher<'a>],
    pub ellipsis_pos: Option<usize>,
}
Expand description

A shape pattern, which is a sequence of terms that can match a shape.

Fields§

§index: &'a [&'a str]

The slot index of the contract.

§terms: &'a [DimMatcher<'a>]

The terms in the pattern.

§ellipsis_pos: Option<usize>

The position of the ellipsis in the pattern, if any.

Implementations§

Source§

impl<'a> ShapeContract<'a>

Source

pub const fn new(index: &'a [&'a str], terms: &'a [DimMatcher<'a>]) -> Self

Create a new shape pattern from a slice of terms.

§Arguments
  • terms: a slice of ShapePatternTerm that defines the pattern.
§Returns

A new ShapePattern instance.

§Macro Support

Consider using the crate::shape_contract macro instead.

use bimm_contracts::{shape_contract, ShapeContract};

static CONTRACT : ShapeContract = shape_contract![
   ...,
   "height" = "h_wins" * "window",
   "width" = "w_wins" * "window",
   "channels",
];
Source

pub fn maybe_key_to_index(&self, key: &str) -> Option<usize>

Convert a key to an index.

Source

pub fn assert_shape<S>(&'a self, shape: S, env: StackEnvironment<'a>)
where S: ShapeArgument,

Assert that the shape matches the pattern.

§Arguments
  • shape: the shape to match.
  • env: the params which are already bound.
§Panics

If the shape does not match the pattern, or if there is a conflict in the bindings.

§Example
use bimm_contracts::{shape_contract, run_periodically, ShapeContract};

let shape = [1, 2, 3, 2 * 8, 3 * 8, 4];

// Run under backoff amortization.
run_periodically! {{
    // Statically allocated contract.
    static CONTRACT : ShapeContract = shape_contract![
       ...,
       "height" = "h_wins" * "window",
       "width" = "w_wins" * "window",
       "channels",
    ];

    // Assert the shape, given the bindings.
    CONTRACT.assert_shape(
        &shape,
        &[("h_wins", 2), ("w_wins", 3), ("channels", 4)]
    );
}}
Source

pub fn try_assert_shape<S>( &'a self, shape: S, env: StackEnvironment<'a>, ) -> Result<(), String>
where S: ShapeArgument,

Assert that the shape matches the pattern.

§Arguments
  • shape: the shape to match.
  • env: the params which are already bound.
§Returns
  • Ok(()): if the shape matches the pattern.
  • Err(String): if the shape does not match the pattern, with an error message.
§Example
use bimm_contracts::{shape_contract, run_periodically, ShapeContract};

let shape = [1, 2, 3, 2 * 8, 3 * 8, 4];

// Statically allocated contract.
static CONTRACT : ShapeContract = shape_contract![
   ...,
   "height" = "h_wins" * "window",
   "width" = "w_wins" * "window",
   "channels",
];

// Assert the shape, given the bindings; or throw.
CONTRACT.try_assert_shape(
    &shape,
    &[("h_wins", 2), ("w_wins", 3), ("channels", 4)]
).unwrap();
Source

pub fn unpack_shape<S, const K: usize>( &'a self, shape: S, keys: &[&'a str; K], env: StackEnvironment<'a>, ) -> [usize; K]
where S: ShapeArgument,

Match and unpack K keys from a shape pattern.

Wraps try_unpack_shape and panics if the shape does not match.

§Generics
  • K: the length of the keys array.
§Arguments
  • shape: the shape to match.
  • keys: the bound keys to export.
  • env: the params which are already bound.
§Returns

An [usize; K] of the unpacked keys values.

§Panics

If the shape does not match the pattern, or if there is a conflict in the bindings.

§Example
use bimm_contracts::{shape_contract, run_periodically, ShapeContract};

let shape = [1, 2, 3, 2 * 8, 3 * 8, 4];

// Statically allocated contract.
static CONTRACT : ShapeContract = shape_contract![
   ...,
   "height" = "h_wins" * "window",
   "width" = "w_wins" * "window",
   "channels",
];

// Unpack the shape, given the bindings.
let [h, w, c] = CONTRACT.unpack_shape(
    &shape,
    &["h_wins", "w_wins", "channels"],
    &[("window", 8)]
);
assert_eq!(h, 2);
assert_eq!(w, 3);
assert_eq!(c, 4);
Source

pub fn try_unpack_shape<S, const K: usize>( &'a self, shape: S, keys: &[&'a str; K], env: StackEnvironment<'a>, ) -> Result<[usize; K], String>
where S: ShapeArgument,

Try and match and unpack K keys from a shape pattern.

§Generics
  • K: the length of the keys array.
§Arguments
  • shape: the shape to match.
  • keys: the bound keys to export.
  • env: the params which are already bound.
§Returns

A Result<[usize; K], String> of the unpacked keys values.

§Example
use bimm_contracts::{shape_contract, run_periodically, ShapeContract};

let shape = [1, 2, 3, 2 * 8, 3 * 8, 4];

// Statically allocated contract.
static CONTRACT : ShapeContract = shape_contract![
   ...,
   "height" = "h_wins" * "window",
   "width" = "w_wins" * "window",
   "channels",
];

// Unpack the shape, given the bindings; or throw.
let [h, w, c] = CONTRACT.try_unpack_shape(
    &shape,
    &["h_wins", "w_wins", "channels"],
    &[("window", 8)]
).unwrap();
assert_eq!(h, 2);
assert_eq!(w, 3);
assert_eq!(c, 4);
Source

pub fn expect_keys_to_selection<const D: usize>( &'a self, keys: &[&'a str; D], ) -> [usize; D]

Convert a list of keys to a selection.

Source

pub fn _resolve( &'a self, shape: &[usize], env: &mut [Option<isize>], ) -> Result<(), String>

Low-level resolver.

Trait Implementations§

Source§

impl<'a> Clone for ShapeContract<'a>

Source§

fn clone(&self) -> ShapeContract<'a>

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<'a> Debug for ShapeContract<'a>

Source§

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

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

impl Display for ShapeContract<'_>

Source§

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

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

impl<'a> PartialEq for ShapeContract<'a>

Source§

fn eq(&self, other: &ShapeContract<'a>) -> 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<'a> Eq for ShapeContract<'a>

Source§

impl<'a> StructuralPartialEq for ShapeContract<'a>

Auto Trait Implementations§

§

impl<'a> Freeze for ShapeContract<'a>

§

impl<'a> RefUnwindSafe for ShapeContract<'a>

§

impl<'a> Send for ShapeContract<'a>

§

impl<'a> Sync for ShapeContract<'a>

§

impl<'a> Unpin for ShapeContract<'a>

§

impl<'a> UnwindSafe for ShapeContract<'a>

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<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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
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<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V