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>
impl<T, const MAX: usize> Threshold<T, MAX>
sourcepub fn new(k: usize, inner: Vec<T>) -> Result<Self, ThresholdError>
pub fn new(k: usize, inner: Vec<T>) -> Result<Self, ThresholdError>
Constructs a threshold directly from a threshold value and collection.
sourcepub fn from_iter<I: Iterator<Item = T>>(
k: usize,
iter: I
) -> Result<Self, ThresholdError>
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.
sourcepub fn or(left: T, right: T) -> Self
pub fn or(left: T, right: T) -> Self
Constructor for an “or” represented as a 1-of-2 threshold.
sourcepub fn and(left: T, right: T) -> Self
pub fn and(left: T, right: T) -> Self
Constructor for an “and” represented as a 2-of-2 threshold.
sourcepub fn set_maximum<const NEWMAX: usize>(
self
) -> Result<Threshold<T, NEWMAX>, ThresholdError>
pub fn set_maximum<const NEWMAX: usize>( self ) -> Result<Threshold<T, NEWMAX>, ThresholdError>
Changes the type-system-enforced maximum value of the threshold.
sourcepub fn forget_maximum(self) -> Threshold<T, 0>
pub fn forget_maximum(self) -> Threshold<T, 0>
Forgets the type-system-enforced maximum value of the threshold.
sourcepub fn map<U, F: FnMut(T) -> U>(self, mapfn: F) -> Threshold<U, MAX>
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.
sourcepub fn map_ref<U, F: FnMut(&T) -> U>(&self, mapfn: F) -> Threshold<U, MAX>
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.
sourcepub fn translate<U, F, FuncError>(
self,
translatefn: F
) -> Result<Threshold<U, MAX>, FuncError>where
F: FnMut(T) -> Result<U, FuncError>,
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.
sourcepub fn translate_ref<U, F, FuncError>(
&self,
translatefn: F
) -> Result<Threshold<U, MAX>, FuncError>where
F: FnMut(&T) -> Result<U, FuncError>,
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.
sourcepub fn translate_by_index<U, F, FuncError>(
&self,
translatefn: F
) -> Result<Threshold<U, MAX>, FuncError>where
F: FnMut(usize) -> Result<U, FuncError>,
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
.
sourcepub fn map_from_post_order_iter<U: Clone>(
&self,
child_indices: &[usize],
processed: &[U]
) -> Threshold<U, MAX>
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.
sourcepub fn data_mut(&mut self) -> &mut [T]
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).
Trait Implementations§
source§impl<T, const MAX: usize> IntoIterator for Threshold<T, MAX>
impl<T, const MAX: usize> IntoIterator for Threshold<T, MAX>
source§impl<T: Ord, const MAX: usize> Ord for Threshold<T, MAX>
impl<T: Ord, const MAX: usize> Ord for Threshold<T, MAX>
1.21.0 · source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere Self: Sized,
source§impl<T: PartialEq, const MAX: usize> PartialEq<Threshold<T, MAX>> for Threshold<T, MAX>
impl<T: PartialEq, const MAX: usize> PartialEq<Threshold<T, MAX>> for Threshold<T, MAX>
source§impl<T: PartialOrd, const MAX: usize> PartialOrd<Threshold<T, MAX>> for Threshold<T, MAX>
impl<T: PartialOrd, const MAX: usize> PartialOrd<Threshold<T, MAX>> for Threshold<T, MAX>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read more