Struct Tree

Source
pub struct Tree { /* private fields */ }

Implementations§

Source§

impl Tree

Creates a new tree in the Yjs doc with the given container name. The tree owns this Yjs map, and it should not be modified manually.

Source

pub fn new(doc: Arc<Doc>, tree_name: &str) -> Arc<Self>

Examples found in repository?
examples/basic.rs (line 7)
5fn main() -> Result<(), Box<dyn Error>> {
6    let doc = Arc::new(yrs::Doc::new());
7    let tree = Tree::new(doc.clone(), "test");
8    let root = tree.root();
9
10    let _sub = tree.on_change(|e| {
11        let TreeUpdateEvent(tree) = e;
12        println!("{}", tree);
13    });
14
15    println!("Add 1 to ROOT");
16    let node1 = root.create_child_with_id("1")?;
17    println!("Add 2 to ROOT");
18    let node2 = root.create_child_with_id("2")?;
19    println!("Add 3 to 1");
20    let node3 = node1.create_child_with_id("3")?;
21    println!("Add 4 to 2");
22    let node4 = node2.create_child_with_id("4")?;
23
24    println!("Move 3 to 2, index 0");
25    node3.move_to(&node2, Some(0))?;
26
27    println!("Move 1 after 2");
28    node1.move_after(&node2)?;
29
30    println!("Move 4 before 3");
31    node4.move_before(&node3)?;
32
33    Ok(())
34}
More examples
Hide additional examples
examples/sync.rs (line 10)
6fn main() -> Result<(), Box<dyn Error>> {
7    let doc1 = Arc::new(yrs::Doc::new());
8    let doc2 = Arc::new(yrs::Doc::new());
9
10    let tree1 = Tree::new(doc1.clone(), "test");
11    let tree2 = Tree::new(doc2.clone(), "test");
12
13    let root1 = tree1.root();
14
15    let node1 = root1.create_child_with_id("1")?;
16    let node2 = root1.create_child_with_id("2")?;
17    let node3 = node1.create_child_with_id("3")?;
18    let node4 = node2.create_child_with_id("4")?;
19    node3.move_to(&node2, Some(0))?;
20    node1.move_after(&node2)?;
21    node4.move_before(&node3)?;
22
23    println!("Tree 1: \n{}", tree1);
24    println!("Syncing Yjs documents...\n");
25
26    let txn = doc1.transact();
27    let update = txn.encode_state_as_update_v1(&Default::default());
28    drop(txn);
29
30    doc2.transact_mut()
31        .apply_update(Update::decode_v1(&update).unwrap())?;
32
33    println!("Tree 2: \n{}", tree2);
34
35    Ok(())
36}
examples/sync_reconcile.rs (line 10)
6fn main() -> Result<(), Box<dyn Error>> {
7    let doc1 = Arc::new(yrs::Doc::new());
8    let doc2 = Arc::new(yrs::Doc::new());
9
10    let tree1 = Tree::new(doc1.clone(), "test");
11    let tree2 = Tree::new(doc2.clone(), "test");
12
13    let root1 = tree1.root();
14
15    let node1 = root1.create_child_with_id("1")?;
16    let node2 = root1.create_child_with_id("2")?;
17    let node3 = node1.create_child_with_id("3")?;
18    let node4 = node2.create_child_with_id("4")?;
19    node3.move_to(&node2, Some(0))?;
20    node1.move_after(&node2)?;
21    node4.move_before(&node3)?;
22
23    sync_docs(&doc1, &doc2)?;
24
25    // Simulate a cycle created by disparate clients
26    let node3_left = tree1.get_node("3").unwrap();
27    let node4_left = tree1.get_node("4").unwrap();
28    node3_left.move_to(&node4_left, None)?;
29
30    let node3_right = tree2.get_node("3").unwrap();
31    let node4_right = tree2.get_node("4").unwrap();
32    node4_right.move_to(&node3_right, None)?;
33
34    println!("Tree 1: \n{}", tree1);
35    println!("Tree 2: \n{}", tree2);
36
37    println!("Syncing docs...\n");
38    sync_docs(&doc1, &doc2)?;
39
40    println!("Tree 1: \n{}", tree1);
41    println!("Tree 2: \n{}", tree2);
42
43    Ok(())
44}
Source

pub fn root(self: &Arc<Self>) -> Arc<Node>

Returns the root node of the tree.

Examples found in repository?
examples/basic.rs (line 8)
5fn main() -> Result<(), Box<dyn Error>> {
6    let doc = Arc::new(yrs::Doc::new());
7    let tree = Tree::new(doc.clone(), "test");
8    let root = tree.root();
9
10    let _sub = tree.on_change(|e| {
11        let TreeUpdateEvent(tree) = e;
12        println!("{}", tree);
13    });
14
15    println!("Add 1 to ROOT");
16    let node1 = root.create_child_with_id("1")?;
17    println!("Add 2 to ROOT");
18    let node2 = root.create_child_with_id("2")?;
19    println!("Add 3 to 1");
20    let node3 = node1.create_child_with_id("3")?;
21    println!("Add 4 to 2");
22    let node4 = node2.create_child_with_id("4")?;
23
24    println!("Move 3 to 2, index 0");
25    node3.move_to(&node2, Some(0))?;
26
27    println!("Move 1 after 2");
28    node1.move_after(&node2)?;
29
30    println!("Move 4 before 3");
31    node4.move_before(&node3)?;
32
33    Ok(())
34}
More examples
Hide additional examples
examples/sync.rs (line 13)
6fn main() -> Result<(), Box<dyn Error>> {
7    let doc1 = Arc::new(yrs::Doc::new());
8    let doc2 = Arc::new(yrs::Doc::new());
9
10    let tree1 = Tree::new(doc1.clone(), "test");
11    let tree2 = Tree::new(doc2.clone(), "test");
12
13    let root1 = tree1.root();
14
15    let node1 = root1.create_child_with_id("1")?;
16    let node2 = root1.create_child_with_id("2")?;
17    let node3 = node1.create_child_with_id("3")?;
18    let node4 = node2.create_child_with_id("4")?;
19    node3.move_to(&node2, Some(0))?;
20    node1.move_after(&node2)?;
21    node4.move_before(&node3)?;
22
23    println!("Tree 1: \n{}", tree1);
24    println!("Syncing Yjs documents...\n");
25
26    let txn = doc1.transact();
27    let update = txn.encode_state_as_update_v1(&Default::default());
28    drop(txn);
29
30    doc2.transact_mut()
31        .apply_update(Update::decode_v1(&update).unwrap())?;
32
33    println!("Tree 2: \n{}", tree2);
34
35    Ok(())
36}
examples/sync_reconcile.rs (line 13)
6fn main() -> Result<(), Box<dyn Error>> {
7    let doc1 = Arc::new(yrs::Doc::new());
8    let doc2 = Arc::new(yrs::Doc::new());
9
10    let tree1 = Tree::new(doc1.clone(), "test");
11    let tree2 = Tree::new(doc2.clone(), "test");
12
13    let root1 = tree1.root();
14
15    let node1 = root1.create_child_with_id("1")?;
16    let node2 = root1.create_child_with_id("2")?;
17    let node3 = node1.create_child_with_id("3")?;
18    let node4 = node2.create_child_with_id("4")?;
19    node3.move_to(&node2, Some(0))?;
20    node1.move_after(&node2)?;
21    node4.move_before(&node3)?;
22
23    sync_docs(&doc1, &doc2)?;
24
25    // Simulate a cycle created by disparate clients
26    let node3_left = tree1.get_node("3").unwrap();
27    let node4_left = tree1.get_node("4").unwrap();
28    node3_left.move_to(&node4_left, None)?;
29
30    let node3_right = tree2.get_node("3").unwrap();
31    let node4_right = tree2.get_node("4").unwrap();
32    node4_right.move_to(&node3_right, None)?;
33
34    println!("Tree 1: \n{}", tree1);
35    println!("Tree 2: \n{}", tree2);
36
37    println!("Syncing docs...\n");
38    sync_docs(&doc1, &doc2)?;
39
40    println!("Tree 1: \n{}", tree1);
41    println!("Tree 2: \n{}", tree2);
42
43    Ok(())
44}
Source

pub fn has_node(self: &Arc<Self>, id: &str) -> bool

Returns true if the tree has a node with the given id.

Source

pub fn get_node(self: &Arc<Self>, id: &str) -> Option<Arc<Node>>

Returns the node with the given id.

Examples found in repository?
examples/sync_reconcile.rs (line 26)
6fn main() -> Result<(), Box<dyn Error>> {
7    let doc1 = Arc::new(yrs::Doc::new());
8    let doc2 = Arc::new(yrs::Doc::new());
9
10    let tree1 = Tree::new(doc1.clone(), "test");
11    let tree2 = Tree::new(doc2.clone(), "test");
12
13    let root1 = tree1.root();
14
15    let node1 = root1.create_child_with_id("1")?;
16    let node2 = root1.create_child_with_id("2")?;
17    let node3 = node1.create_child_with_id("3")?;
18    let node4 = node2.create_child_with_id("4")?;
19    node3.move_to(&node2, Some(0))?;
20    node1.move_after(&node2)?;
21    node4.move_before(&node3)?;
22
23    sync_docs(&doc1, &doc2)?;
24
25    // Simulate a cycle created by disparate clients
26    let node3_left = tree1.get_node("3").unwrap();
27    let node4_left = tree1.get_node("4").unwrap();
28    node3_left.move_to(&node4_left, None)?;
29
30    let node3_right = tree2.get_node("3").unwrap();
31    let node4_right = tree2.get_node("4").unwrap();
32    node4_right.move_to(&node3_right, None)?;
33
34    println!("Tree 1: \n{}", tree1);
35    println!("Tree 2: \n{}", tree2);
36
37    println!("Syncing docs...\n");
38    sync_docs(&doc1, &doc2)?;
39
40    println!("Tree 1: \n{}", tree1);
41    println!("Tree 2: \n{}", tree2);
42
43    Ok(())
44}
Source

pub fn on_change( &self, callback: impl Fn(&TreeUpdateEvent) + Send + Sync + 'static, ) -> Subscription

Returns a subscription to the tree. When dropped, the subscription is automatically cancelled.

Examples found in repository?
examples/basic.rs (lines 10-13)
5fn main() -> Result<(), Box<dyn Error>> {
6    let doc = Arc::new(yrs::Doc::new());
7    let tree = Tree::new(doc.clone(), "test");
8    let root = tree.root();
9
10    let _sub = tree.on_change(|e| {
11        let TreeUpdateEvent(tree) = e;
12        println!("{}", tree);
13    });
14
15    println!("Add 1 to ROOT");
16    let node1 = root.create_child_with_id("1")?;
17    println!("Add 2 to ROOT");
18    let node2 = root.create_child_with_id("2")?;
19    println!("Add 3 to 1");
20    let node3 = node1.create_child_with_id("3")?;
21    println!("Add 4 to 2");
22    let node4 = node2.create_child_with_id("4")?;
23
24    println!("Move 3 to 2, index 0");
25    node3.move_to(&node2, Some(0))?;
26
27    println!("Move 1 after 2");
28    node1.move_after(&node2)?;
29
30    println!("Move 4 before 3");
31    node4.move_before(&node3)?;
32
33    Ok(())
34}
Source

pub fn traverse_dfs(self: &Arc<Self>) -> DfsIter

Returns an iterator over the nodes in the tree in depth-first order.

Trait Implementations§

Source§

impl Clone for Tree

Source§

fn clone(&self) -> Tree

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 Tree

Source§

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

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

impl Display for Tree

Source§

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

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

impl PartialEq for Tree

Source§

fn eq(&self, other: &Self) -> 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.

Auto Trait Implementations§

§

impl !Freeze for Tree

§

impl !RefUnwindSafe for Tree

§

impl !Send for Tree

§

impl !Sync for Tree

§

impl Unpin for Tree

§

impl !UnwindSafe for Tree

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, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.