tea_core/vec_core/
getlen.rs

1/// A trait for types that have a length.
2///
3/// This trait provides methods to get the length of a collection-like object
4/// and to check if it's empty.
5pub trait GetLen {
6    /// Returns the number of elements in the collection.
7    ///
8    /// # Returns
9    ///
10    /// The number of elements in the collection.
11    fn len(&self) -> usize;
12
13    /// Checks if the collection is empty.
14    ///
15    /// # Returns
16    ///
17    /// `true` if the collection contains no elements, `false` otherwise.
18    #[inline]
19    fn is_empty(&self) -> bool {
20        self.len() == 0
21    }
22}
23
24impl<T: GetLen> GetLen for std::sync::Arc<T> {
25    #[inline]
26    fn len(&self) -> usize {
27        self.as_ref().len()
28    }
29}
30
31impl<T: GetLen> GetLen for Box<T> {
32    #[inline]
33    fn len(&self) -> usize {
34        self.as_ref().len()
35    }
36}
37
38impl<T: GetLen> GetLen for &T {
39    #[inline]
40    fn len(&self) -> usize {
41        (*self).len()
42    }
43}
44
45impl<T: GetLen> GetLen for &mut T {
46    #[inline]
47    fn len(&self) -> usize {
48        (**self).len()
49    }
50}