Struct miniscript::Threshold

source ·
pub struct Threshold<T, const MAX: usize> { /* private fields */ }
Expand description

Structure representing a k-of-n threshold collection of some arbitrary object T.

If the constant parameter MAX is nonzero, it represents a cap on the n value; if n exceeds MAX then an error is returned on construction.

Implementations§

source§

impl<T, const MAX: usize> Threshold<T, MAX>

source

pub fn new(k: usize, inner: Vec<T>) -> Result<Self, ThresholdError>

Constructs a threshold directly from a threshold value and collection.

source

pub fn from_iter<I: Iterator<Item = T>>( k: usize, iter: I ) -> Result<Self, ThresholdError>

Constructs a threshold from a threshold value and an iterator that yields collection elements.

source

pub fn or(left: T, right: T) -> Self

Constructor for an “or” represented as a 1-of-2 threshold.

source

pub fn and(left: T, right: T) -> Self

Constructor for an “and” represented as a 2-of-2 threshold.

source

pub fn is_or(&self) -> bool

Whether this threshold is a 1-of-n.

source

pub fn is_and(&self) -> bool

Whether this threshold is a n-of-n.

source

pub fn set_maximum<const NEWMAX: usize>( self ) -> Result<Threshold<T, NEWMAX>, ThresholdError>

Changes the type-system-enforced maximum value of the threshold.

source

pub fn forget_maximum(self) -> Threshold<T, 0>

Forgets the type-system-enforced maximum value of the threshold.

source

pub fn map<U, F: FnMut(T) -> U>(self, mapfn: F) -> Threshold<U, MAX>

Constructs a threshold from an existing threshold by applying a mapping function to each individual item.

source

pub fn map_ref<U, F: FnMut(&T) -> U>(&self, mapfn: F) -> Threshold<U, MAX>

Like Self::map but takes a reference to the threshold rather than taking ownership.

source

pub fn translate<U, F, FuncError>( self, translatefn: F ) -> Result<Threshold<U, MAX>, FuncError>
where F: FnMut(T) -> Result<U, FuncError>,

Like Self::map except that the mapping function may return an error.

source

pub fn translate_ref<U, F, FuncError>( &self, translatefn: F ) -> Result<Threshold<U, MAX>, FuncError>
where F: FnMut(&T) -> Result<U, FuncError>,

Like Self::translate but takes a reference to the threshold rather than taking ownership.

source

pub fn translate_by_index<U, F, FuncError>( &self, translatefn: F ) -> Result<Threshold<U, MAX>, FuncError>
where F: FnMut(usize) -> Result<U, FuncError>,

Like Self::translate_ref but passes indices to the closure rather than internal data.

This is useful in situations where the data to be translated exists outside of the threshold itself, and the threshold data is irrelevant. In particular it is commonly paired with crate::expression::Tree::to_null_threshold.

If the data to be translated comes from a post-order iterator, you may instead want Self::map_from_post_order_iter.

source

pub fn map_from_post_order_iter<U: Clone>( &self, child_indices: &[usize], processed: &[U] ) -> Threshold<U, MAX>

Construct a threshold from an existing threshold which has been processed in some way.

It is a common pattern in this library to transform data structures by running a post-order iterator over them, putting processed elements into a vector to be later referenced by their parents.

This function encapsulates that pattern by taking the child-index vector of thePostOrderIterItem, under consideration, and the vector of processed elements.

source

pub fn n(&self) -> usize

Accessor for the number of elements in the threshold.

source

pub const fn k(&self) -> usize

Accessor for the threshold value.

source

pub fn data(&self) -> &[T]

Accessor for the underlying data.

source

pub fn data_mut(&mut self) -> &mut [T]

Mutable accessor for the underlying data.

This returns access to the underlying data as a mutable slice, which allows you to modify individual elements. To change the number of elements, you must destructure the threshold with Self::k and Self::into_data and reconstruct it (and on reconstruction, deal with any errors caused by your tinkering with the threshold values).

source

pub fn into_data(self) -> Vec<T>

Accessor for the underlying data.

source

pub fn iter(&self) -> Iter<'_, T>

Passthrough to an iterator on the underlying vector.

source§

impl<T> Threshold<T, 0>

source

pub fn or_n(inner: Vec<T>) -> Self

Constructor for an “or” represented as a 1-of-n threshold.

§Panics

Panics if the passed vector is empty.

source

pub fn and_n(inner: Vec<T>) -> Self

Constructor for an “and” represented as a n-of-n threshold.

§Panics

Panics if the passed vector is empty.

source§

impl<T: Display, const MAX: usize> Threshold<T, MAX>

source

pub fn display<'s>(&'s self, name: &'s str, show_k: bool) -> impl Display + 's

Produces an object which can fmt::Display the threshold.

source§

impl<T: Debug, const MAX: usize> Threshold<T, MAX>

source

pub fn debug<'s>(&'s self, name: &'s str, show_k: bool) -> impl Debug + 's

Produces an object which can fmt::Debug the threshold.

Trait Implementations§

source§

impl<T: Clone, const MAX: usize> Clone for Threshold<T, MAX>

source§

fn clone(&self) -> Threshold<T, MAX>

Returns a copy 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<T: Debug, const MAX: usize> Debug for Threshold<T, MAX>

source§

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

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

impl<T: Hash, const MAX: usize> Hash for Threshold<T, MAX>

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<T, const MAX: usize> IntoIterator for Threshold<T, MAX>

§

type Item = T

The type of the elements being iterated over.
§

type IntoIter = IntoIter<T>

Which kind of iterator are we turning this into?
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl<T: Ord, const MAX: usize> Ord for Threshold<T, MAX>

source§

fn cmp(&self, other: &Threshold<T, MAX>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized + PartialOrd,

Restrict a value to a certain interval. Read more
source§

impl<T: PartialEq, const MAX: usize> PartialEq for Threshold<T, MAX>

source§

fn eq(&self, other: &Threshold<T, MAX>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<T: PartialOrd, const MAX: usize> PartialOrd for Threshold<T, MAX>

source§

fn partial_cmp(&self, other: &Threshold<T, MAX>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

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

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

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

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

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

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

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

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl<T: Eq, const MAX: usize> Eq for Threshold<T, MAX>

source§

impl<T, const MAX: usize> StructuralPartialEq for Threshold<T, MAX>

Auto Trait Implementations§

§

impl<T, const MAX: usize> Freeze for Threshold<T, MAX>

§

impl<T, const MAX: usize> RefUnwindSafe for Threshold<T, MAX>
where T: RefUnwindSafe,

§

impl<T, const MAX: usize> Send for Threshold<T, MAX>
where T: Send,

§

impl<T, const MAX: usize> Sync for Threshold<T, MAX>
where T: Sync,

§

impl<T, const MAX: usize> Unpin for Threshold<T, MAX>
where T: Unpin,

§

impl<T, const MAX: usize> UnwindSafe for Threshold<T, MAX>
where T: UnwindSafe,

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

§

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, U> TryFrom<U> for T
where U: Into<T>,

§

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

§

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