Struct afrim_memory::Node

source ·
pub struct Node {
    pub depth: usize,
    pub key: char,
    /* private fields */
}
Expand description

A node in the text buffer.

             0 ----------------> The root node
            / \
          'g' 's' -------------> Node: Rc<Node>
          /     \
  "ɣ" = '+'     'h' -----------> Node: Rc<Node>
                  \
                  '+' = "ʃ" ---> Node that holds a value

Fields§

§depth: usize

Depth of the node.

§key: char

Character holded by the node.

Implementations§

source§

impl Node

source

pub fn new(key: char, depth: usize) -> Self

Initializes a new node in the text buffer.

Can also be used to initialize the text buffer (not recommanded). Uses Node::default instead.

§Example
use afrim_memory::Node;

let text_buffer = Node::new('\0', 0);

// You cannot assign directly a value to a node.
// But, an alternative is as below.
let node = Node::new('u', 0);
node.insert(vec![], "ʉ̠̀".to_owned());
assert_eq!(node.take(), Some("ʉ̠̀".to_owned()));

Note: Early, Node::new was the only way to initialize a text buffer but it has been replaced by Node::default which is now more adapted for this use case.

source

pub fn insert(&self, sequence: Vec<char>, value: String)

Inserts a sequence in the text buffer.

§Example
use afrim_memory::Node;

let text_buffer = Node::default();
text_buffer.insert(vec!['.', 't'], "ṫ".to_owned());

let node = text_buffer.goto('.').and_then(|node| node.goto('t'));
assert_eq!(node.unwrap().take(), Some("ṫ".to_owned()));
source

pub fn goto(&self, character: char) -> Option<Rc<Self>>

Moves from the current node to his child.

Useful to go through a sequence.

§Example
use afrim_memory::Node;

let text_buffer = Node::default();
text_buffer.insert(vec!['o', '/'], "ø".to_owned());
text_buffer.insert(vec!['o', '*'], "ɔ".to_owned());
text_buffer.insert(vec!['o', '1'], "ò".to_owned());
text_buffer.insert(vec!['o', '*', '~'], "ɔ̃".to_owned());

// let sequence = ['o', '*', '~'];
let node = text_buffer.goto('o').unwrap();
assert_eq!(node.take(), None);
let node = node.goto('*').unwrap();
assert_eq!(node.take(), Some("ɔ".to_owned()));
let node = node.goto('~').unwrap();
assert_eq!(node.take(), Some("ɔ̃".to_owned()));
source

pub fn take(&self) -> Option<String>

Extracts the value of the node.

A node in the text buffer don’t always holds a value. Hence, his value is optional.

§Example
use afrim_memory::Node;

let text_buffer = Node::default();
text_buffer.insert(vec!['1', 'c'], "c̀".to_string());

let node = text_buffer.goto('1').unwrap();
assert_eq!(node.take(), None);
let node = node.goto('c').unwrap();
assert_eq!(node.take(), Some("c̀".to_owned()));
source

pub fn is_root(&self) -> bool

Returns true is the node is at the initial depth.

Useful when dealing with the Cursor. Will permit to know the beginning and the end of a sequence.

§Example
use afrim_memory::{Cursor, Node};

let text_buffer = Node::default();
text_buffer.insert(vec!['e', '2' ], "é".to_owned());
text_buffer.insert(vec!['i', '7' ], "ǐ".to_owned());

assert!(text_buffer.is_root());
let node = text_buffer.goto('e').unwrap();
assert!(!node.is_root());

Trait Implementations§

source§

impl Clone for Node

source§

fn clone(&self) -> Node

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 Node

source§

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

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

impl Default for Node

source§

fn default() -> Self

Create a root node.

A root node always holds a null character as key and is recommanded to use to initialize the text buffer. You should always use it to create a text buffer because the internal code can change.

§Example
use afrim_memory::Node;

// It's recommanded to use it, to initialize your text buffer.
let text_buffer = Node::default();
// Not recommanded.
let another_text_buffer = Node::new('\0', 0);

assert!(text_buffer.is_root());
assert!(another_text_buffer.is_root());

Auto Trait Implementations§

§

impl !RefUnwindSafe for Node

§

impl !Send for Node

§

impl !Sync for Node

§

impl Unpin for Node

§

impl !UnwindSafe for Node

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

§

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

§

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

§

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.