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.
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.
Sourcepub fn new(doc: Arc<Doc>, tree_name: &str) -> Arc<Self>
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
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}
Sourcepub fn root(self: &Arc<Self>) -> Arc<Node>
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
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}
Sourcepub fn has_node(self: &Arc<Self>, id: &str) -> bool
pub fn has_node(self: &Arc<Self>, id: &str) -> bool
Returns true if the tree has a node with the given id.
Sourcepub fn get_node(self: &Arc<Self>, id: &str) -> Option<Arc<Node>>
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}
Sourcepub fn on_change(
&self,
callback: impl Fn(&TreeUpdateEvent) + Send + Sync + 'static,
) -> Subscription
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}
Sourcepub fn traverse_dfs(self: &Arc<Self>) -> DfsIter
pub fn traverse_dfs(self: &Arc<Self>) -> DfsIter
Returns an iterator over the nodes in the tree in depth-first order.
Trait Implementations§
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more