Skip to main content

optionstratlib_core/utils/
traits.rs

1/// A trait for types that have a notion of length or size.
2///
3/// This trait provides a standard interface for determining the number of elements
4/// in a collection or the size of an object. It defines both a required `len()` method
5/// and a default implementation of `is_empty()` which relies on `len()`.
6///
7/// Types implementing this trait can be checked for emptiness using the `is_empty()`
8/// method without requiring a separate implementation, as long as they provide
9/// a way to determine their length.
10///
11pub trait Len {
12    /// Returns the number of elements in the collection or the size of the object.
13    ///
14    /// # Returns
15    ///
16    /// A `usize` representing the length or size.
17    fn len(&self) -> usize;
18
19    /// Returns `true` if the collection contains no elements or the object has zero size.
20    ///
21    /// # Returns
22    ///
23    /// A boolean indicating whether the object is empty.
24    fn is_empty(&self) -> bool {
25        self.len() == 0
26    }
27}