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>
impl<'a> ShapeContract<'a>
Sourcepub const fn new(index: &'a [&'a str], terms: &'a [DimMatcher<'a>]) -> Self
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 ofShapePatternTermthat 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",
];Sourcepub fn maybe_key_to_index(&self, key: &str) -> Option<usize>
pub fn maybe_key_to_index(&self, key: &str) -> Option<usize>
Convert a key to an index.
Sourcepub fn assert_shape<S>(&'a self, shape: S, env: StackEnvironment<'a>)where
S: ShapeArgument,
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)]
);
}}Sourcepub fn try_assert_shape<S>(
&'a self,
shape: S,
env: StackEnvironment<'a>,
) -> Result<(), String>where
S: ShapeArgument,
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();Sourcepub fn unpack_shape<S, const K: usize>(
&'a self,
shape: S,
keys: &[&'a str; K],
env: StackEnvironment<'a>,
) -> [usize; K]where
S: ShapeArgument,
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 thekeysarray.
§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);Sourcepub 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,
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 thekeysarray.
§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);Trait Implementations§
Source§impl<'a> Clone for ShapeContract<'a>
impl<'a> Clone for ShapeContract<'a>
Source§fn clone(&self) -> ShapeContract<'a>
fn clone(&self) -> ShapeContract<'a>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<'a> Debug for ShapeContract<'a>
impl<'a> Debug for ShapeContract<'a>
Source§impl Display for ShapeContract<'_>
impl Display for ShapeContract<'_>
Source§impl<'a> PartialEq for ShapeContract<'a>
impl<'a> PartialEq for ShapeContract<'a>
impl<'a> Eq for ShapeContract<'a>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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