[][src]Struct id_tree::NodeId

pub struct NodeId { /* fields omitted */ }

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

impl Ord for NodeId[src]

fn max(self, other: Self) -> Self1.21.0[src]

Compares and returns the maximum of two values. Read more

fn min(self, other: Self) -> Self1.21.0[src]

Compares and returns the minimum of two values. Read more

fn clamp(self, min: Self, max: Self) -> Self[src]

🔬 This is a nightly-only experimental API. (clamp)

Restrict a value to a certain interval. Read more

impl PartialOrd<NodeId> for NodeId[src]

impl PartialEq<NodeId> for NodeId[src]

impl Clone for NodeId[src]

fn clone_from(&mut self, source: &Self)1.0.0[src]

Performs copy-assignment from source. Read more

impl Eq for NodeId[src]

impl Debug for NodeId[src]

impl Hash for NodeId[src]

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

Feeds a slice of this type into the given [Hasher]. Read more

Auto Trait Implementations

impl Send for NodeId

impl Unpin for NodeId

impl Sync for NodeId

impl UnwindSafe for NodeId

impl RefUnwindSafe for NodeId

Blanket Implementations

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]