Struct NodeId

Source
pub struct NodeId { /* private fields */ }
Expand description

An identifier used to differentiate between Nodes within a Tree.

NodeIds are not something that the calling context will ever have to worry about generating. Trees generate NodeIds as Nodes are inserted into them.

In addition, each NodeId is specific to the Tree that generated it. This means that if there are two Trees - A and B - there’s no worry of trying to access a Node in A with an identifier that came from B. Doing so will return a Result::Err value instead of returning the wrong Node.

§Potential NodeId Issues

Because Trees pass out NodeIds as Nodes are inserted, several issues can occur:

  1. If a Node is removed, the NodeId that previously identified it now points to nothing (technically a None value in this case).
  2. If a Node is removed and then another is inserted later, the “new” NodeId that is returned can (and will) be the same NodeId that was used to identify a different Node previously.

The above issues may seem like deal-breakers, but our situation isn’t as bad as it seems:

The first issue can be easily detected by the library itself. In this situation, a Result::Err will be returned with the appropriate NodeIdError. The second issue, however, is not something that the library can detect. To mitigate this problem, this library ensures the following:

  1. All Node methods that provide NodeIds will return &NodeIds instead of NodeIds.
  2. All Tree methods that read or insert data accept &NodeIds instead of taking NodeIds.
  3. All Tree methods that remove data take NodeIds instead of accepting &NodeIds.
  4. All Nodes that have been removed from a Tree will have their parent and child references cleared (to avoid leaking extra NodeId copies).
  5. NodeIds themselves are Clone, but not Copy.

This means that no methods will ever take ownership of a NodeId except for methods that remove a Node from a Tree. The resulting behavior is that unless the caller explicitly Clones a NodeId they should never be in a situation where they accidentally hold onto a NodeId too long. This means that we have “almost safe references” that the caller can clone if they choose to. Doing so, however, will open up the possibility of confusing which NodeIds refer to which Nodes in the calling context.

This does transfer some of the burden to the caller, but any errors should be fairly easy to sort out because an explicit Clone is required for such an error to occur.

Trait Implementations§

Source§

impl Clone for NodeId

Source§

fn clone(&self) -> NodeId

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 Debug for NodeId

Source§

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

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

impl Hash for NodeId

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Ord for NodeId

Source§

fn cmp(&self, other: &NodeId) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

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

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

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

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

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

impl PartialEq for NodeId

Source§

fn eq(&self, other: &NodeId) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for NodeId

Source§

fn partial_cmp(&self, other: &NodeId) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

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

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

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Eq for NodeId

Source§

impl StructuralPartialEq for NodeId

Auto Trait Implementations§

§

impl Freeze for NodeId

§

impl RefUnwindSafe for NodeId

§

impl Send for NodeId

§

impl Sync for NodeId

§

impl Unpin for NodeId

§

impl UnwindSafe for NodeId

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

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

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 T
where T: Clone,

Source§

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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.
Source§

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

Source§

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

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.