y-rb 0.3.2-x86_64-linux → 0.4.0-x86_64-linux

Sign up to get free protection for your applications and to get access to all the features.
data/ext/yrb/src/ymap.rs CHANGED
@@ -6,35 +6,47 @@ use magnus::block::Proc;
6
6
  use magnus::{Error, RArray, RHash, Symbol, Value};
7
7
  use std::cell::RefCell;
8
8
  use yrs::types::{EntryChange, Value as YrsValue};
9
- use yrs::Map;
9
+ use yrs::{Map, MapRef, Observable};
10
10
 
11
11
  #[magnus::wrap(class = "Y::Map")]
12
- pub(crate) struct YMap(pub(crate) RefCell<Map>);
12
+ pub(crate) struct YMap(pub(crate) RefCell<MapRef>);
13
13
 
14
14
  /// SAFETY: This is safe because we only access this data when the GVL is held.
15
15
  unsafe impl Send for YMap {}
16
16
 
17
17
  impl YMap {
18
18
  pub(crate) fn ymap_clear(&self, transaction: &YTransaction) {
19
- self.0.borrow_mut().clear(&mut *transaction.0.borrow_mut());
19
+ let mut tx = transaction.transaction();
20
+ let tx = tx.as_mut().unwrap();
21
+
22
+ self.0.borrow_mut().clear(tx)
20
23
  }
21
- pub(crate) fn ymap_contains(&self, key: Value) -> bool {
24
+ pub(crate) fn ymap_contains(&self, transaction: &YTransaction, key: Value) -> bool {
25
+ let tx = transaction.transaction();
26
+ let tx = tx.as_ref().unwrap();
27
+
22
28
  match indifferent_hash_key(key) {
23
29
  None => false,
24
- Some(k) => self.0.borrow().contains(&*k),
30
+ Some(k) => self.0.borrow().contains(tx, k.as_str()),
25
31
  }
26
32
  }
27
- pub(crate) fn ymap_each(&self, proc: Proc) {
28
- self.0.borrow().iter().for_each(|(key, val)| {
33
+ pub(crate) fn ymap_each(&self, transaction: &YTransaction, proc: Proc) {
34
+ let tx = transaction.transaction();
35
+ let tx = tx.as_ref().unwrap();
36
+
37
+ self.0.borrow().iter(tx).for_each(|(key, val)| {
29
38
  let k = key.to_string();
30
39
  let v = *YValue::from(val).0.borrow();
31
40
  proc.call::<(String, Value), Value>((k, v))
32
41
  .expect("cannot iterate map");
33
- });
42
+ })
34
43
  }
35
- pub(crate) fn ymap_get(&self, key: Value) -> Option<Value> {
44
+ pub(crate) fn ymap_get(&self, transaction: &YTransaction, key: Value) -> Option<Value> {
45
+ let tx = transaction.transaction();
46
+ let tx = tx.as_ref().unwrap();
47
+
36
48
  indifferent_hash_key(key)
37
- .map(|k| self.0.borrow().get(&*k))
49
+ .map(|k| self.0.borrow().get(tx, k.as_str()))
38
50
  .map(|v| v.unwrap_or(YrsValue::Any(Any::Undefined)))
39
51
  .map(|v| *YValue::from(v).0.borrow())
40
52
  }
@@ -44,15 +56,16 @@ impl YMap {
44
56
  key: Value,
45
57
  value: Value,
46
58
  ) -> Result<(), Error> {
59
+ let mut tx = transaction.transaction();
60
+ let tx = tx.as_mut().unwrap();
61
+
47
62
  match indifferent_hash_key(key) {
48
63
  None => Err(Error::runtime_error(
49
- "invalid key type, make sure it is either a Symbol or a String",
64
+ "invalid key type, make sure it is either of type Symbol or String",
50
65
  )),
51
66
  Some(k) => {
52
67
  let v = Any::from(YValue::from(value));
53
- self.0
54
- .borrow_mut()
55
- .insert(&mut *transaction.0.borrow_mut(), k, v);
68
+ self.0.borrow_mut().insert(tx, k, v);
56
69
 
57
70
  Ok(())
58
71
  }
@@ -62,7 +75,6 @@ impl YMap {
62
75
  let change_inserted = Symbol::new("inserted").as_static();
63
76
  let change_updated = Symbol::new("updated").as_static();
64
77
  let change_removed = Symbol::new("removed").as_static();
65
-
66
78
  self.0
67
79
  .borrow_mut()
68
80
  .observe(move |transaction, map_event| {
@@ -125,23 +137,28 @@ impl YMap {
125
137
  .into()
126
138
  }
127
139
  pub(crate) fn ymap_remove(&self, transaction: &YTransaction, key: Value) -> Option<Value> {
140
+ let mut tx = transaction.transaction();
141
+ let tx = tx.as_mut().unwrap();
142
+
128
143
  indifferent_hash_key(key)
129
- .map(|k| {
130
- self.0
131
- .borrow()
132
- .remove(&mut *transaction.0.borrow_mut(), &*k)
133
- })
144
+ .map(|k| self.0.borrow().remove(tx, k.as_str()))
134
145
  .map(|v| v.unwrap_or(YrsValue::Any(Any::Undefined)))
135
146
  .map(|v| *YValue::from(v).0.borrow())
136
147
  }
137
- pub(crate) fn ymap_size(&self) -> u32 {
138
- self.0.borrow().len()
148
+ pub(crate) fn ymap_size(&self, transaction: &YTransaction) -> u32 {
149
+ let tx = transaction.transaction();
150
+ let tx = tx.as_ref().unwrap();
151
+
152
+ self.0.borrow().len(tx)
139
153
  }
140
- pub(crate) fn ymap_to_h(&self) -> RHash {
154
+ pub(crate) fn ymap_to_h(&self, transaction: &YTransaction) -> RHash {
155
+ let tx = transaction.transaction();
156
+ let tx = tx.as_ref().unwrap();
157
+
141
158
  RHash::from_iter(
142
159
  self.0
143
160
  .borrow()
144
- .iter()
161
+ .iter(tx)
145
162
  .map(move |(k, v)| (k.to_string(), *YValue::from(v).0.borrow())),
146
163
  )
147
164
  }
@@ -149,3 +166,9 @@ impl YMap {
149
166
  self.0.borrow_mut().unobserve(subscription_id);
150
167
  }
151
168
  }
169
+
170
+ impl From<MapRef> for YMap {
171
+ fn from(v: MapRef) -> Self {
172
+ YMap(RefCell::from(v))
173
+ }
174
+ }
data/ext/yrb/src/ytext.rs CHANGED
@@ -7,10 +7,10 @@ use magnus::value::Qnil;
7
7
  use magnus::{Error, RHash, Symbol, Value};
8
8
  use std::cell::RefCell;
9
9
  use yrs::types::Delta;
10
- use yrs::Text;
10
+ use yrs::{GetString, Observable, Text, TextRef};
11
11
 
12
12
  #[magnus::wrap(class = "Y::Text")]
13
- pub(crate) struct YText(pub(crate) RefCell<Text>);
13
+ pub(crate) struct YText(pub(crate) RefCell<TextRef>);
14
14
 
15
15
  /// SAFETY: This is safe because we only access this data when the GVL is held.
16
16
  unsafe impl Send for YText {}
@@ -22,40 +22,33 @@ impl YText {
22
22
  index: u32,
23
23
  length: u32,
24
24
  attrs: RHash,
25
- ) -> Result<(), Error> {
25
+ ) {
26
+ let mut tx = transaction.transaction();
27
+ let tx = tx.as_mut().unwrap();
28
+
26
29
  let a = YAttrs::from(attrs);
27
- self.0
28
- .borrow_mut()
29
- .format(&mut *transaction.0.borrow_mut(), index, length, a.0);
30
30
 
31
- Ok(())
31
+ self.0.borrow_mut().format(tx, index, length, a.0)
32
32
  }
33
- pub(crate) fn ytext_insert(
34
- &self,
35
- transaction: &YTransaction,
36
- index: u32,
37
- chunk: String,
38
- ) -> Result<(), Error> {
39
- self.0
40
- .borrow_mut()
41
- .insert(&mut *transaction.0.borrow_mut(), index, &*chunk);
33
+ pub(crate) fn ytext_insert(&self, transaction: &YTransaction, index: u32, chunk: String) {
34
+ let mut tx = transaction.transaction();
35
+ let tx = tx.as_mut().unwrap();
42
36
 
43
- Ok(())
37
+ self.0.borrow_mut().insert(tx, index, chunk.as_str())
44
38
  }
45
39
  pub(crate) fn ytext_insert_embed(
46
40
  &self,
47
41
  transaction: &YTransaction,
48
42
  index: u32,
49
43
  content: Value,
50
- ) -> Result<(), Error> {
44
+ ) {
45
+ let mut tx = transaction.transaction();
46
+ let tx = tx.as_mut().unwrap();
47
+
51
48
  let yvalue = YValue::from(content);
52
49
  let avalue = Any::from(yvalue);
53
50
 
54
- self.0
55
- .borrow_mut()
56
- .insert_embed(&mut *transaction.0.borrow_mut(), index, avalue);
57
-
58
- Ok(())
51
+ self.0.borrow_mut().insert_embed(tx, index, avalue)
59
52
  }
60
53
  pub(crate) fn ytext_insert_embed_with_attributes(
61
54
  &self,
@@ -63,20 +56,18 @@ impl YText {
63
56
  index: u32,
64
57
  embed: Value,
65
58
  attrs: RHash,
66
- ) -> Result<(), Error> {
59
+ ) {
60
+ let mut tx = transaction.transaction();
61
+ let tx = tx.as_mut().unwrap();
62
+
67
63
  let yvalue = YValue::from(embed);
68
64
  let avalue = Any::from(yvalue);
69
65
 
70
66
  let a = YAttrs::from(attrs);
71
67
 
72
- self.0.borrow_mut().insert_embed_with_attributes(
73
- &mut *transaction.0.borrow_mut(),
74
- index,
75
- avalue,
76
- a.0,
77
- );
78
-
79
- Ok(())
68
+ self.0
69
+ .borrow_mut()
70
+ .insert_embed_with_attributes(tx, index, avalue, a.0)
80
71
  }
81
72
  pub(crate) fn ytext_insert_with_attributes(
82
73
  &self,
@@ -84,20 +75,21 @@ impl YText {
84
75
  index: u32,
85
76
  chunk: String,
86
77
  attrs: RHash,
87
- ) -> Result<(), Error> {
88
- let a = YAttrs::from(attrs);
78
+ ) {
79
+ let mut tx = transaction.transaction();
80
+ let tx = tx.as_mut().unwrap();
89
81
 
90
- self.0.borrow_mut().insert_with_attributes(
91
- &mut *transaction.0.borrow_mut(),
92
- index,
93
- &*chunk,
94
- a.0,
95
- );
82
+ let a = YAttrs::from(attrs);
96
83
 
97
- Ok(())
84
+ self.0
85
+ .borrow_mut()
86
+ .insert_with_attributes(tx, index, chunk.as_str(), a.0)
98
87
  }
99
- pub(crate) fn ytext_length(&self) -> u32 {
100
- self.0.borrow().len()
88
+ pub(crate) fn ytext_length(&self, transaction: &YTransaction) -> u32 {
89
+ let tx = transaction.transaction();
90
+ let tx = tx.as_ref().unwrap();
91
+
92
+ self.0.borrow().len(tx)
101
93
  }
102
94
  pub(crate) fn ytext_observe(&self, block: Proc) -> Result<u32, Error> {
103
95
  let delta_insert = Symbol::new("insert").to_static();
@@ -106,7 +98,6 @@ impl YText {
106
98
  let attributes = Symbol::new("attributes").to_static();
107
99
 
108
100
  // let mut error: Option<Error> = None;
109
-
110
101
  let subscription_id = self
111
102
  .0
112
103
  .borrow_mut()
@@ -179,26 +170,30 @@ impl YText {
179
170
  Ok(subscription_id)
180
171
  }
181
172
  pub(crate) fn ytext_push(&self, transaction: &YTransaction, chunk: String) {
182
- self.0
183
- .borrow_mut()
184
- .push(&mut *transaction.0.borrow_mut(), &*chunk);
173
+ let mut tx = transaction.transaction();
174
+ let tx = tx.as_mut().unwrap();
175
+
176
+ self.0.borrow_mut().push(tx, chunk.as_str())
185
177
  }
186
- pub(crate) fn ytext_remove_range(
187
- &self,
188
- transaction: &YTransaction,
189
- start: u32,
190
- length: u32,
191
- ) -> Result<(), Error> {
192
- self.0
193
- .borrow_mut()
194
- .remove_range(&mut *transaction.0.borrow_mut(), start, length);
178
+ pub(crate) fn ytext_remove_range(&self, transaction: &YTransaction, start: u32, length: u32) {
179
+ let mut tx = transaction.transaction();
180
+ let tx = tx.as_mut().unwrap();
195
181
 
196
- Ok(())
182
+ self.0.borrow_mut().remove_range(tx, start, length)
197
183
  }
198
- pub(crate) fn ytext_to_s(&self) -> String {
199
- return self.0.borrow().to_string();
184
+ pub(crate) fn ytext_to_s(&self, transaction: &YTransaction) -> String {
185
+ let mut tx = transaction.transaction();
186
+ let tx = tx.as_mut().unwrap();
187
+
188
+ self.0.borrow().get_string(tx)
200
189
  }
201
190
  pub(crate) fn ytext_unobserve(&self, subscription_id: u32) {
202
- return self.0.borrow_mut().unobserve(subscription_id);
191
+ self.0.borrow_mut().unobserve(subscription_id);
192
+ }
193
+ }
194
+
195
+ impl From<TextRef> for YText {
196
+ fn from(v: TextRef) -> Self {
197
+ YText(RefCell::from(v))
203
198
  }
204
199
  }
@@ -2,54 +2,102 @@ use crate::yarray::YArray;
2
2
  use crate::ymap::YMap;
3
3
  use crate::ytext::YText;
4
4
  use crate::yxml_element::YXmlElement;
5
+ use crate::yxml_fragment::YXmlFragment;
5
6
  use crate::yxml_text::YXmlText;
6
7
  use magnus::Error;
7
- use std::cell::RefCell;
8
+ use std::cell::{RefCell, RefMut};
8
9
  use yrs::updates::decoder::Decode;
9
10
  use yrs::updates::encoder::Encode;
10
- use yrs::{Transaction, Update};
11
+ use yrs::{ReadTxn, TransactionMut, Update};
11
12
 
12
13
  #[magnus::wrap(class = "Y::Transaction")]
13
- pub(crate) struct YTransaction(pub(crate) RefCell<Transaction>);
14
+ pub(crate) struct YTransaction(pub(crate) RefCell<Option<TransactionMut<'static>>>);
14
15
 
15
16
  /// SAFETY: This is safe because we only access this data when the GVL is held.
16
17
  unsafe impl Send for YTransaction {}
17
18
 
19
+ impl YTransaction {}
20
+
21
+ impl<'doc> From<TransactionMut<'doc>> for YTransaction {
22
+ fn from(txn: TransactionMut<'doc>) -> Self {
23
+ let txn: TransactionMut<'static> = unsafe { std::mem::transmute(txn) };
24
+ YTransaction(RefCell::from(Some(txn)))
25
+ }
26
+ }
27
+
28
+ // API which is eventually publicly exposed
18
29
  impl YTransaction {
19
30
  pub(crate) fn ytransaction_apply_update(&self, update: Vec<u8>) -> Result<(), Error> {
20
- return Update::decode_v1(update.as_slice())
21
- .map(|u| self.0.borrow_mut().apply_update(u))
22
- .map_err(|_e| Error::runtime_error("cannot apply update"));
31
+ Update::decode_v1(update.as_slice())
32
+ .map_err(|error| Error::runtime_error(format!("cannot decode update: {:?}", error)))
33
+ .map(|u| self.transaction().as_mut().unwrap().apply_update(u))
23
34
  }
35
+
24
36
  pub(crate) fn ytransaction_commit(&self) {
25
- self.0.borrow_mut().commit();
37
+ self.transaction().as_mut().unwrap().commit();
38
+ }
39
+
40
+ pub(crate) fn ytransaction_get_array(&self, name: String) -> Option<YArray> {
41
+ self.transaction()
42
+ .as_ref()
43
+ .unwrap()
44
+ .get_array(name.as_str())
45
+ .map(YArray::from)
26
46
  }
27
- pub(crate) fn ytransaction_get_array(&self, name: String) -> YArray {
28
- let a = self.0.borrow_mut().get_array(&*name);
29
47
 
30
- YArray(RefCell::from(a))
48
+ pub(crate) fn ytransaction_get_map(&self, name: String) -> Option<YMap> {
49
+ self.transaction()
50
+ .as_ref()
51
+ .unwrap()
52
+ .get_map(name.as_str())
53
+ .map(YMap::from)
31
54
  }
32
- pub(crate) fn ytransaction_get_map(&self, name: String) -> YMap {
33
- let m = self.0.borrow_mut().get_map(&*name);
34
55
 
35
- YMap(RefCell::from(m))
56
+ pub(crate) fn ytransaction_get_text(&self, name: String) -> Option<YText> {
57
+ self.transaction()
58
+ .as_ref()
59
+ .unwrap()
60
+ .get_text(name.as_str())
61
+ .map(YText::from)
36
62
  }
37
- pub(crate) fn ytransaction_get_text(&self, name: String) -> YText {
38
- let t = self.0.borrow_mut().get_text(&*name);
39
63
 
40
- YText(RefCell::new(t))
64
+ pub(crate) fn ytransaction_get_xml_element(&self, name: String) -> Option<YXmlElement> {
65
+ self.transaction()
66
+ .as_ref()
67
+ .unwrap()
68
+ .get_xml_element(name.as_str())
69
+ .map(YXmlElement::from)
41
70
  }
42
- pub(crate) fn ytransaction_get_xml_element(&self, name: String) -> YXmlElement {
43
- let el = self.0.borrow_mut().get_xml_element(&*name);
44
71
 
45
- YXmlElement(RefCell::new(el))
72
+ pub(crate) fn ytransaction_get_xml_fragment(&self, name: String) -> Option<YXmlFragment> {
73
+ self.transaction()
74
+ .as_ref()
75
+ .unwrap()
76
+ .get_xml_fragment(name.as_str())
77
+ .map(YXmlFragment::from)
46
78
  }
47
- pub(crate) fn ytransaction_get_xml_text(&self, name: String) -> YXmlText {
48
- let t = self.0.borrow_mut().get_xml_text(&*name);
49
79
 
50
- YXmlText(RefCell::new(t))
80
+ pub(crate) fn ytransaction_get_xml_text(&self, name: String) -> Option<YXmlText> {
81
+ self.transaction()
82
+ .as_ref()
83
+ .unwrap()
84
+ .get_xml_text(name.as_str())
85
+ .map(YXmlText::from)
51
86
  }
87
+
52
88
  pub(crate) fn ytransaction_state_vector(&self) -> Vec<u8> {
53
- return self.0.borrow_mut().state_vector().encode_v1();
89
+ self.transaction()
90
+ .as_ref()
91
+ .unwrap()
92
+ .state_vector()
93
+ .encode_v1()
94
+ }
95
+
96
+ pub(crate) fn ytransaction_free(&self) {
97
+ self.0.replace(None);
98
+ }
99
+
100
+ pub(crate) fn transaction(&self) -> RefMut<'_, Option<TransactionMut<'static>>> {
101
+ self.0.borrow_mut()
54
102
  }
55
103
  }
@@ -6,7 +6,10 @@ use magnus::{class, Float, Integer, RArray, RHash, RString, Symbol, Value, QNIL}
6
6
  use std::cell::RefCell;
7
7
  use std::collections::HashMap;
8
8
  use yrs::types::Value as YrsValue;
9
- use yrs::{Text as YrsText, XmlElement as YrsXmlElement, XmlText as YrsXmlText};
9
+ use yrs::{
10
+ Array, Map, TextRef as YrsText, Transact, XmlElementRef as YrsXmlElement,
11
+ XmlTextRef as YrsXmlText,
12
+ };
10
13
 
11
14
  pub(crate) struct YValue(pub(crate) RefCell<Value>);
12
15
 
@@ -141,12 +144,28 @@ impl From<YrsValue> for YValue {
141
144
  YrsValue::YText(text) => YValue::from(text),
142
145
  YrsValue::YXmlElement(el) => YValue::from(el),
143
146
  YrsValue::YXmlText(text) => YValue::from(text),
144
- // YrsValue::YArray(val) => YValue::from(RArray::from_vec(val.iter().map(|item| {
145
- // let yvalue = YValue::from(item);
146
- // *yvalue.0
147
- // }))),
148
- // YrsValue::YMap(val) => YValue::from(RHash::from_iter(val.iter())),
149
- v => panic!("cannot map complex yrs values to yvalue: {}", v.to_string()),
147
+ YrsValue::YArray(val) => {
148
+ let tx = val.transact();
149
+ let arr = RArray::from_vec(
150
+ val.iter(&tx)
151
+ .map(|item| {
152
+ let yvalue = YValue::from(item);
153
+ yvalue.0.into_inner()
154
+ })
155
+ .collect(),
156
+ );
157
+ YValue::from(arr)
158
+ }
159
+ YrsValue::YMap(val) => {
160
+ let tx = val.transact();
161
+ let iter = val.iter(&tx).map(|(key, val)| {
162
+ let val = YValue::from(val);
163
+ let val = val.0.into_inner();
164
+ (key, val)
165
+ });
166
+ YValue::from(RHash::from_iter(iter))
167
+ }
168
+ v => panic!("cannot map complex yrs values to yvalue: {:?}", v),
150
169
  }
151
170
  }
152
171
  }