Struct tskit::NodeId

source ·
#[repr(transparent)]
pub struct NodeId(_);
Expand description

A node ID

This is an integer referring to a row of a NodeTable. The underlying type is tsk_id_t.

Examples

These examples illustrate using this type as something “integer-like”.

use tskit::NodeId;
use tskit::bindings::tsk_id_t;

let x: tsk_id_t = 1;
let y: NodeId = NodeId::from(x);
assert_eq!(x, y);
assert_eq!(y, x);

assert!(y < x + 1);
assert!(y <= x);
assert!(x + 1 > y);
assert!(x + 1 >= y);

let z: NodeId = NodeId::from(x);
assert_eq!(y, z);

It is also possible to write functions accepting both the NodeId and tsk_id_t:

use tskit::NodeId;
use tskit::bindings::tsk_id_t;

fn interesting<N: Into<NodeId>>(x: N) -> NodeId {
    x.into()
}

let x: tsk_id_t = 0;
assert_eq!(interesting(x), x);
let x: NodeId = NodeId::from(0);
assert_eq!(interesting(x), x);

The types also implement Display:

use tskit::NodeId;

let n = NodeId::from(11);
assert_eq!(format!("{}", n), "11".to_string());
// Debug output contains type info
assert_eq!(format!("{:?}", n), "NodeId(11)".to_string());
let n = NodeId::from(NodeId::NULL);
assert_eq!(format!("{}", n), "NULL");
assert_eq!(format!("{:?}", n), "NodeId(-1)");

Implementations§

NULL value for this type.

Return true is self == Self::NULL

Examples found in repository?
src/tree_interface.rs (line 610)
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
    fn new(tree: &'a TreeInterface) -> Self {
        debug_assert!(tree.right_child(tree.virtual_root()).is_some());
        let mut rv = PreorderNodeIterator {
            current_root: tree
                .right_child(tree.virtual_root())
                .unwrap_or(NodeId::NULL),
            node_stack: vec![],
            tree,
            current_node_: None,
        };
        let mut c = rv.current_root;
        while !c.is_null() {
            rv.node_stack.push(c);
            debug_assert!(rv.tree.left_sib(c).is_some());
            c = rv.tree.left_sib(c).unwrap_or(NodeId::NULL);
        }
        rv
    }

Convenience function to convert to usize.

Works via TryFrom.

Returns
  • None if the underlying value is negative.
  • Some(usize) otherwise.

Convenience function to convert to usize. Implemented via as. Negative values with therefore wrap.

Examples found in repository?
src/tree_interface.rs (line 518)
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
    pub fn total_branch_length(&self, by_span: bool) -> Result<Time, TskitError> {
        let time: &[Time] = sys::generate_slice(
            unsafe {
                (*(*(*self.non_owned_pointer.as_ptr()).tree_sequence).tables)
                    .nodes
                    .time
            },
            self.num_nodes,
        );
        let mut b = Time::from(0.);
        for n in self.traverse_nodes(NodeTraversalOrder::Preorder) {
            let p = self.parent(n).ok_or(TskitError::IndexError {})?;
            if p != NodeId::NULL {
                b += time[p.as_usize()] - time[n.as_usize()]
            }
        }

        match by_span {
            true => Ok(b * self.span()),
            false => Ok(b),
        }
    }

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Formats the value using the given formatter. Read more
Converts to this type from the input type.
Converts to this type from the input type.
Feeds this value into the given Hasher. Read more
Feeds a slice of this type into the given Hasher. Read more
This method returns an Ordering between self and other. Read more
Compares and returns the maximum of two values. Read more
Compares and returns the minimum of two values. Read more
Restrict a value to a certain interval. Read more
This method tests for self and other values to be equal, and is used by ==.
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
This method tests for self and other values to be equal, and is used by ==.
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
This method tests for self and other values to be equal, and is used by ==.
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
This method tests for self and other values to be equal, and is used by ==.
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
This method tests for self and other values to be equal, and is used by ==.
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
This method returns an ordering between self and other values if one exists. Read more
This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
This method returns an ordering between self and other values if one exists. Read more
This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
This method returns an ordering between self and other values if one exists. Read more
This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
This method returns an ordering between self and other values if one exists. Read more
This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
This method returns an ordering between self and other values if one exists. Read more
This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more
Drops the content pointed by this pointer and frees it. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
Converts the given value to a String. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.