Struct granular_id::GranularId

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

source

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.3
source

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.6
source

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.3
source§

impl<T> GranularId<T>where T: BackSequentialId,

source

pub fn previous_siblings(&self) -> BackSequentialIds<T> where T: Bounded + One + Clone + CheckedSub,

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 siblings
source§

impl<T> GranularId<T>

source

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());
source

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);
source

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());
source

pub fn into_parent(self) -> 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

use granular_id::GranularId;
let id: GranularId<u8> = vec![1, 2, 3].into(); // id 1.2.3
assert_eq!(id.into_parent(), vec![1, 2].into()); // id 1.2
source

pub fn parent(&self) -> Selfwhere T: Clone,

Gets the parent of this GranularId, that is, the same GranularId with the last component removed.

use granular_id::GranularId;
let id: GranularId<u8> = vec![1, 2, 3].into(); // id 1.2.3
assert_eq!(id.parent(), vec![1, 2].into()); // id 1.2
source

pub fn pop(&mut self)

Removes the last component of this GranularId. To get the parent of any GranularId without mutating it, see GranularId::parent

use granular_id::GranularId;
let mut id: GranularId<u8> = vec![1, 2, 3].into(); // id 1.2.3
id.pop();
assert_eq!(id, vec![1, 2].into()); // id 1.2
source

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.4
source

pub fn pushing(&self, component: T) -> Selfwhere T: UpperBounded + PartialEq + Clone,

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.4
source

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.6
source

pub fn appending(&self, other: GranularId<T>) -> GranularId<T>where T: UpperBounded + PartialEq + Clone,

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.6
source

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.6
source

pub fn appending_vec(&self, other: Vec<T>) -> GranularId<T>where T: UpperBounded + PartialEq + Clone,

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.6
source

pub fn root() -> Self

Gets the root GranularId of any type, that is, a GranularId without any components. That GranularId is the smallest one of that type.

use granular_id::GranularId;
let id: GranularId<u8> = GranularId::root(); // id <root>
assert_eq!(Into::<Vec<u8>>::into(id), vec![]);

Trait Implementations§

source§

impl<T: Clone> Clone for GranularId<T>

source§

fn clone(&self) -> GranularId<T>

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> Debug for GranularId<T>

source§

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

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

impl<T: Default> Default for GranularId<T>

source§

fn default() -> GranularId<T>

Returns the “default value” for a type. Read more
source§

impl<T: Display> Display for GranularId<T>

source§

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

A GranularId is displayed similarly to a version string, where the GranularId created from the vector [1, 2, 3] is displayed as 1.2.3. If the GranularId is the root ID (doesn’t have any components), it is displayed as <root>.

source§

impl<T> From<&[T]> for GranularId<T>where T: PartialEq + UpperBounded + Clone,

source§

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>

source§

fn from(other: GranularId<T>) -> Vec<T>

Turns a GranularId into a vector of its components.

source§

impl<T> From<Vec<T, Global>> for GranularId<T>where T: PartialEq + UpperBounded,

source§

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> IntoIterator for GranularId<T>

source§

fn into_iter(self) -> Self::IntoIter

Turns this GranularId into an iterator over its components, consuming them.

§

type Item = T

The type of the elements being iterated over.
§

type IntoIter = <Vec<T, Global> as IntoIterator>::IntoIter

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

impl<T> LowerBounded for GranularId<T>

source§

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,

source§

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,

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

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

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

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

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

impl<T: PartialEq> PartialEq<GranularId<T>> for GranularId<T>

source§

fn eq(&self, other: &GranularId<T>) -> 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<GranularId<T>> for GranularId<T>where T: Ord,

source§

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

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> UpperBounded for GranularId<T>where T: UpperBounded,

source§

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.

source§

impl<T: Eq> Eq for GranularId<T>

source§

impl<T> StructuralEq for GranularId<T>

source§

impl<T> StructuralPartialEq for GranularId<T>

Auto Trait Implementations§

§

impl<T> RefUnwindSafe for GranularId<T>where T: RefUnwindSafe,

§

impl<T> Send for GranularId<T>where T: Send,

§

impl<T> Sync for GranularId<T>where T: Sync,

§

impl<T> Unpin for GranularId<T>where T: Unpin,

§

impl<T> UnwindSafe for GranularId<T>where T: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · 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 Twhere 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> ToString for Twhere T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.