pub struct GranularId<T> { /* private fields */ }Expand description
The data type GranularId represents any ID which can have an arbitrary granularity,
meaning that there is indefinitely many GranularIds in-between any two GranularIds. It is
best used with unsized integer types such as u8, u16, u32, u64 and usize, but may be
used with other data types implementing the required
num_traits bounds.
An ID basically consists of multiple components
Implementations§
Source§impl<T> GranularId<T>where
T: SequentialId,
impl<T> GranularId<T>where
T: SequentialId,
Sourcepub fn all_siblings(&self) -> SequentialIds<T> ⓘwhere
T: LowerBounded,
pub fn all_siblings(&self) -> SequentialIds<T> ⓘwhere
T: LowerBounded,
Gets all the siblings of this GranularId, ordered from the smallest sibling to the largest
sibling. This is all the GranularIds which starts the same as this GranularId but has
its last component changed.
use granular_id::GranularId;
let id = GranularId::from(vec![1u8, 2, 2]); // id 1.2.2
let mut all_siblings = id.all_siblings();
assert_eq!(all_siblings.next().unwrap(), vec![1, 2, 0].into()); // id 1.2.0
assert_eq!(all_siblings.next().unwrap(), vec![1, 2, 1].into()); // id 1.2.1
assert_eq!(all_siblings.next().unwrap(), vec![1, 2, 2].into()); // id 1.2.2
assert_eq!(all_siblings.next().unwrap(), vec![1, 2, 3].into()); // id 1.2.3Sourcepub fn next_siblings(&self) -> SequentialIds<T> ⓘwhere
T: CheckedAdd,
pub fn next_siblings(&self) -> SequentialIds<T> ⓘwhere
T: CheckedAdd,
Gets all the next siblings of this GranularId, ordered from the smallest sibling larger
than this ID to the largest sibling. This is all the GranularIds which starts the same as
this GranularId but has its last component changed to any larger value.
use granular_id::GranularId;
let id = GranularId::from(vec![1u8, 2, 2]); // id 1.2.2
let mut next_siblings = id.next_siblings();
assert_eq!(next_siblings.next().unwrap(), vec![1, 2, 3].into()); // id 1.2.3
assert_eq!(next_siblings.next().unwrap(), vec![1, 2, 4].into()); // id 1.2.4
assert_eq!(next_siblings.next().unwrap(), vec![1, 2, 5].into()); // id 1.2.5
assert_eq!(next_siblings.next().unwrap(), vec![1, 2, 6].into()); // id 1.2.6Sourcepub fn children(&self) -> SequentialIds<T> ⓘwhere
T: LowerBounded,
pub fn children(&self) -> SequentialIds<T> ⓘwhere
T: LowerBounded,
Gets all direct children of this GranularId, ordered from the smallest child (which is
larger than this ID, but smallest out of all children) to the largest child. This is all
the GranularIds which starts with this GranularId but has one additional component.
use granular_id::GranularId;
let id = GranularId::from(vec![1u8, 2]); // id 1.2
let mut children = id.children();
assert_eq!(children.next().unwrap(), vec![1, 2, 0].into()); // id 1.2.0
assert_eq!(children.next().unwrap(), vec![1, 2, 1].into()); // id 1.2.1
assert_eq!(children.next().unwrap(), vec![1, 2, 2].into()); // id 1.2.2
assert_eq!(children.next().unwrap(), vec![1, 2, 3].into()); // id 1.2.3Source§impl<T> GranularId<T>where
T: BackSequentialId,
impl<T> GranularId<T>where
T: BackSequentialId,
Sourcepub fn previous_siblings(&self) -> BackSequentialIds<T> ⓘ
pub fn previous_siblings(&self) -> BackSequentialIds<T> ⓘ
Gets all the previous siblings of this GranularId, ordered from the largest sibling
smaller than this ID to the smallest sibling. This is all the GranularIds which starts the
same as this GranularId but has its last component changed to any smaller value.
use granular_id::GranularId;
let id = GranularId::from(vec![1u8, 2, 3]); // id 1.2.3
let mut previous_siblings = id.previous_siblings();
assert_eq!(previous_siblings.next().unwrap(), vec![1, 2, 2].into()); // id 1.2.2
assert_eq!(previous_siblings.next().unwrap(), vec![1, 2, 1].into()); // id 1.2.1
assert_eq!(previous_siblings.next().unwrap(), vec![1, 2, 0].into()); // id 1.2.0
assert_eq!(previous_siblings.next(), None); // No more smaller siblingsSource§impl<T> GranularId<T>
impl<T> GranularId<T>
Sourcepub fn is_max_value(&self) -> boolwhere
T: UpperBounded + PartialEq,
pub fn is_max_value(&self) -> boolwhere
T: UpperBounded + PartialEq,
Checks whether this GranularId is the maximum value of this type. If it is, no
other larger GranularIds exist of the same type.
use granular_id::GranularId;
let not_max: GranularId<u8> = vec![254].into();
let max: GranularId<u8> = vec![255].into();
assert!(!not_max.is_max_value());
assert!(max.is_max_value());Sourcepub fn granularity(&self) -> usize
pub fn granularity(&self) -> usize
Gets the granularity of this GranularId, which is the number of components it contains,
or the “precision” it has
use granular_id::GranularId;
let id: GranularId<u8> = vec![1, 2, 3, 4].into(); // id 1.2.3.4, granularity 4
assert_eq!(id.granularity(), 4);
// Its children should have granularity 5:
assert_eq!(id.children().next().unwrap().granularity(), 5);Sourcepub fn is_root(&self) -> bool
pub fn is_root(&self) -> bool
Checks whether this GranularId is the root, which means not having any components.
use granular_id::GranularId;
let not_root: GranularId<u8> = vec![254].into();
let root: GranularId<u8> = vec![].into();
assert!(!not_root.is_root());
assert!(root.is_root());Sourcepub fn into_parent(self) -> Option<Self>
pub fn into_parent(self) -> Option<Self>
Turns this GranularId into its parent, that is, the same GranularId with the last
component removed. If this GranularId is the root (has no components), this function
returns the root. For a mutating version, see GranularId::pop, and for a copying
version, see GranularId::parent. If this GranularId doesn’t have any components (is
the root ID), this function returns None since the root ID doesn’t have a parent.
use granular_id::GranularId;
let id: GranularId<u8> = vec![1, 2, 3].into(); // id 1.2.3
assert_eq!(id.into_parent(), Some(vec![1, 2].into())); // id 1.2Sourcepub fn parent(&self) -> Option<Self>where
T: Clone,
pub fn parent(&self) -> Option<Self>where
T: Clone,
Gets the parent of this GranularId, that is, the same GranularId with the last component
removed. If this GranularId doesn’t have any components (is the root ID), this function
returns None since the root ID doesn’t have a parent.
use granular_id::GranularId;
let id: GranularId<u8> = vec![1, 2, 3].into(); // id 1.2.3
assert_eq!(id.parent(), Some(vec![1, 2].into())); // id 1.2Sourcepub fn into_ancestors(self) -> Ancestors<T> ⓘ
pub fn into_ancestors(self) -> Ancestors<T> ⓘ
Returns an iterator over the ancestors of this GranularId. The iterator will yield all the
ancestors, in order from the next-most parent until the root GranularId. This will give the
same results as iteratively calling GranularId::parent. This function consumes the
GranularId.
use granular_id::GranularId;
let id: GranularId<u8> = vec![1, 2, 3].into(); // id 1.2.3
let mut ancestors = id.into_ancestors();
assert_eq!(ancestors.next(), Some(vec![1, 2].into())); // id 1.2
assert_eq!(ancestors.next(), Some(vec![1].into())); // id 1
assert_eq!(ancestors.next(), Some(vec![].into())); // root id
assert_eq!(ancestors.next(), None); // There are no more ancestors at this pointSourcepub fn ancestors(&self) -> Ancestors<T> ⓘwhere
T: Clone,
pub fn ancestors(&self) -> Ancestors<T> ⓘwhere
T: Clone,
Returns an iterator over the ancestors of this GranularId. The iterator will yield all the
ancestors, in order from the next-most parent until the root GranularId. This will give the
same results as iteratively calling GranularId::parent.
use granular_id::GranularId;
let id: GranularId<u8> = vec![1, 2, 3].into(); // id 1.2.3
let mut ancestors = id.ancestors();
assert_eq!(ancestors.next(), Some(vec![1, 2].into())); // id 1.2
assert_eq!(ancestors.next(), Some(vec![1].into())); // id 1
assert_eq!(ancestors.next(), Some(vec![].into())); // root id
assert_eq!(ancestors.next(), None); // There are no more parents at this pointSourcepub fn pop(&mut self) -> Option<T>
pub fn pop(&mut self) -> Option<T>
Removes the last component of this GranularId. To get the parent of any GranularId
without mutating it, see GranularId::parent. The function returns the component popped,
akin to Vec::pop, which is None if this GranularId doesn’t have a parent. In that
case, this GranularId will remain unchanged as the root.
use granular_id::GranularId;
let mut id: GranularId<u8> = vec![1, 2, 3].into(); // id 1.2.3
assert_eq!(id.pop(), Some(3)); // popping the last component '3'
assert_eq!(id, vec![1, 2].into()); // id 1.2Sourcepub fn push(&mut self, component: T)where
T: UpperBounded + PartialEq,
pub fn push(&mut self, component: T)where
T: UpperBounded + PartialEq,
Pushes a new component to the GranularId, adding it as a new last component. Since the
maximum bound for a GranularId of type T is [T::max], if any component is pushed to
such an ID, the call doesn’t do anything.
use granular_id::GranularId;
let mut id: GranularId<u8> = vec![1, 2, 3].into(); // id 1.2.3
id.push(4);
assert_eq!(id, vec![1, 2, 3, 4].into()); // id 1.2.3.4Sourcepub fn pushing(&self, component: T) -> Self
pub fn pushing(&self, component: T) -> Self
Makes a new GranularId, adding the given component as its new last component. Since the
maximum bound for a GranularId of type T is [T::max], if any component is pushed to
such an ID, the call returns a plain copy of the original GranularId.
use granular_id::GranularId;
let mut id: GranularId<u8> = vec![1, 2, 3].into(); // id 1.2.3
assert_eq!(id.pushing(4), vec![1, 2, 3, 4].into()); // id 1.2.3.4Sourcepub fn append(&mut self, other: GranularId<T>)where
T: UpperBounded + PartialEq,
pub fn append(&mut self, other: GranularId<T>)where
T: UpperBounded + PartialEq,
Appends all components from another GranularId to this GranularId, adding them as a new
last components. Since the maximum bound for a GranularId of type T is [T::max], if
any components is appended to such an ID, the call doesn’t do anything.
use granular_id::GranularId;
let mut id: GranularId<u8> = vec![1, 2, 3].into(); // id 1.2.3
let id_2 = vec![4, 5, 6].into(); // id 4.5.6
id.append(id_2);
assert_eq!(id, vec![1, 2, 3, 4, 5, 6].into()); // id 1.2.3.4.5.6Sourcepub fn appending(&self, other: GranularId<T>) -> GranularId<T>
pub fn appending(&self, other: GranularId<T>) -> GranularId<T>
Makes a new GranularId, appending all components from another GranularId to that
GranularId, adding them as a new last components. Since the maximum bound for a
GranularId of type T is [T::max], if any components is appended to such an ID, the
call returns a copy of the original GranularId.
use granular_id::GranularId;
let mut id: GranularId<u8> = vec![1, 2, 3].into(); // id 1.2.3
let id_2 = vec![4, 5, 6].into(); // id 4.5.6
assert_eq!(id.appending(id_2), vec![1, 2, 3, 4, 5, 6].into()); // id 1.2.3.4.5.6Sourcepub fn append_vec(&mut self, other: Vec<T>)where
T: UpperBounded + PartialEq,
pub fn append_vec(&mut self, other: Vec<T>)where
T: UpperBounded + PartialEq,
Appends all components from a Vec to this GranularId, adding them as a new
last components. Since the maximum bound for a GranularId of type T is [T::max], if
any components is appended to such an ID, the call doesn’t do anything.
use granular_id::GranularId;
let mut id: GranularId<u8> = vec![1, 2, 3].into(); // id 1.2.3
id.append_vec(vec![4, 5, 6]);
assert_eq!(id, vec![1, 2, 3, 4, 5, 6].into()); // id 1.2.3.4.5.6Sourcepub fn appending_vec(&self, other: Vec<T>) -> GranularId<T>
pub fn appending_vec(&self, other: Vec<T>) -> GranularId<T>
Makes a new GranularId, appending all components from a Vec to that
GranularId, adding them as a new last components. Since the maximum bound for a
GranularId of type T is [T::max], if any components is appended to such an ID, the
call returns a copy of the original GranularId.
use granular_id::GranularId;
let mut id: GranularId<u8> = vec![1, 2, 3].into(); // id 1.2.3
assert_eq!(id.appending_vec(vec![4, 5, 6]), vec![1, 2, 3, 4, 5, 6].into()); // id 1.2.3.4.5.6Trait Implementations§
Source§impl<T: Clone> Clone for GranularId<T>
impl<T: Clone> Clone for GranularId<T>
Source§fn clone(&self) -> GranularId<T>
fn clone(&self) -> GranularId<T>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<T: Debug> Debug for GranularId<T>
impl<T: Debug> Debug for GranularId<T>
Source§impl<T: Default> Default for GranularId<T>
impl<T: Default> Default for GranularId<T>
Source§fn default() -> GranularId<T>
fn default() -> GranularId<T>
Source§impl<T: Display> Display for GranularId<T>
impl<T: Display> Display for GranularId<T>
Source§impl<T> From<&[T]> for GranularId<T>
impl<T> From<&[T]> for GranularId<T>
Source§fn from(other: &[T]) -> Self
fn from(other: &[T]) -> Self
A slice of components may be turned into a GranularId. Keep in mind that the maximum
GranularId of any type T is the one containing just the component T::max, attempting
to convert any vector starting with T::max will result in the
upper bound GranularId for that type.
Source§impl<T> From<GranularId<T>> for Vec<T>
impl<T> From<GranularId<T>> for Vec<T>
Source§fn from(other: GranularId<T>) -> Vec<T>
fn from(other: GranularId<T>) -> Vec<T>
Turns a GranularId into a vector of its components.
Source§impl<T> From<Vec<T>> for GranularId<T>where
T: PartialEq + UpperBounded,
impl<T> From<Vec<T>> for GranularId<T>where
T: PartialEq + UpperBounded,
Source§fn from(other: Vec<T>) -> Self
fn from(other: Vec<T>) -> Self
A vector of components may be turned into a GranularId. Keep in mind that the maximum
GranularId of any type T is the one containing just the component T::max, attempting
to convert any vector starting with T::max will result in the
upper bound GranularId for that type.
Source§impl<T: Hash> Hash for GranularId<T>
impl<T: Hash> Hash for GranularId<T>
Source§impl<T> IntoIterator for GranularId<T>
impl<T> IntoIterator for GranularId<T>
Source§impl<T> LowerBounded for GranularId<T>
impl<T> LowerBounded for GranularId<T>
Source§fn min_value() -> Self
fn min_value() -> Self
The lower bound for any GranularId (the smallest ID) is the empty ID which contains no
components.
Source§impl<T> Ord for GranularId<T>where
T: Ord,
impl<T> Ord for GranularId<T>where
T: Ord,
Source§fn cmp(&self, other: &Self) -> Ordering
fn cmp(&self, other: &Self) -> Ordering
When comparing two GranularIds, their relative positioning is determined by its first
non-matching component. All ID:s starting with 1 is less than all ID:s starting with a 2
and 1.2 is ordered before 1.2.0. Here is an example of some ordered GranularIds:
use num_traits::bounds::LowerBounded;
use granular_id::GranularId;
let vec: Vec<GranularId<u8>> = vec![
vec![1].into(), // id 1
vec![1, 0].into(), // id 1.0
vec![1, 1].into(), // id 1.1
vec![1, 1, 0].into(), // id 1.1.0
vec![1, 1, 1].into(), // id 1.1.1
vec![1, 1, 2].into(), // id 1.1.2
vec![1, 2].into(), // id 1.2
vec![2].into(), // id 2
];
let mut min = GranularId::min_value();
for id in vec {
assert!(id > min);
min = id;
}For a GranularId to have a total ordering, the component type must also have a total
ordering.
1.21.0 · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl<T: PartialEq> PartialEq for GranularId<T>
impl<T: PartialEq> PartialEq for GranularId<T>
Source§impl<T> PartialOrd for GranularId<T>where
T: Ord,
impl<T> PartialOrd for GranularId<T>where
T: Ord,
Source§fn partial_cmp(&self, other: &Self) -> Option<Ordering>
fn partial_cmp(&self, other: &Self) -> Option<Ordering>
Since GranularIds does have a total ordering if and only if its type has a total ordering,
this function never returns None and just returns the value GranularId<T>::cmp would
return
Source§impl<T> UpperBounded for GranularId<T>where
T: UpperBounded,
impl<T> UpperBounded for GranularId<T>where
T: UpperBounded,
Source§fn max_value() -> Self
fn max_value() -> Self
The upper bound for any GranularId (the largest ID) is the empty ID which contains one
component, containing the upper bound of the component type.