fix!: removed canc from factory, impl for Arc<str>

This commit is contained in:
əlemi 2023-09-13 00:05:15 +02:00
parent 5812dea19a
commit c6abc33c53
2 changed files with 8 additions and 22 deletions

View file

@ -2,7 +2,7 @@
//! //!
//! a helper trait that any string container can implement, which generates opseqs //! a helper trait that any string container can implement, which generates opseqs
//! //!
//! an OperationFactory trait implementation is provided for String, but plugin developers //! an OperationFactory trait implementation is provided for `String` and `Arc<str>`, but plugin developers
//! should implement their own operation factory interfacing directly with the editor //! should implement their own operation factory interfacing directly with the editor
//! buffer when possible. //! buffer when possible.
@ -93,16 +93,6 @@ pub fn op_effective_range(op: &OperationSeq) -> Range<u64> {
/// assert_eq!(factory, "from scratch"); /// assert_eq!(factory, "from scratch");
/// # Ok::<(), codemp::ot::OTError>(()) /// # Ok::<(), codemp::ot::OTError>(())
/// ``` /// ```
///
/// use [OperationFactory::canc] to remove characters at index, but backwards
///
/// ```rust
/// # use codemp::api::OperationFactory;
/// # let mut factory = String::from("from scratch");
/// factory = factory.canc(12, 8).apply(&factory)?;
/// assert_eq!(factory, "from");
/// # Ok::<(), codemp::ot::OTError>(())
/// ```
pub trait OperationFactory { pub trait OperationFactory {
/// the current content of the buffer /// the current content of the buffer
fn content(&self) -> String; fn content(&self) -> String;
@ -160,16 +150,6 @@ pub trait OperationFactory {
out.retain(len - (pos+count)); out.retain(len - (pos+count));
out out
} }
/// delete n characters backwards at given position
fn canc(&self, pos: u64, count: u64) -> OperationSeq {
let mut out = OperationSeq::default();
let len = self.content().len() as u64;
out.retain(pos - count);
out.delete(count);
out.retain(len - pos);
out
}
} }
impl OperationFactory for String { impl OperationFactory for String {
@ -177,3 +157,9 @@ impl OperationFactory for String {
self.clone() self.clone()
} }
} }
impl OperationFactory for std::sync::Arc<str> {
fn content(&self) -> String {
self.to_string()
}
}

View file

@ -166,7 +166,7 @@ pub mod sync {
} }
/// return a reference to contained tokio runtime, to spawn tasks on /// return a reference to contained tokio runtime, to spawn tasks on
pub fn rt(&self) -> &Handle { &self.runtime.handle() } pub fn rt(&self) -> &Handle { self.runtime.handle() }
/// connect and store a client session, threadsafe and sync version of [crate::client::Client::new] /// connect and store a client session, threadsafe and sync version of [crate::client::Client::new]
pub fn connect(&self, addr: &str) -> Result<(), Error> { pub fn connect(&self, addr: &str) -> Result<(), Error> {