From 3ae5ba09fdc0b111f3c6200da2c8c1e2a9c6c30f Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Fri, 22 Oct 2021 12:46:02 -0600 Subject: [PATCH] Implement TryFrom on SelectionSet More prep work for changing the selection set representation. --- crates/buffer/src/lib.rs | 48 ++++++----------------------- crates/buffer/src/selection.rs | 55 +++++++++++++++++++++++++++++++--- 2 files changed, 60 insertions(+), 43 deletions(-) diff --git a/crates/buffer/src/lib.rs b/crates/buffer/src/lib.rs index 4511531cf4..6deaf228a1 100644 --- a/crates/buffer/src/lib.rs +++ b/crates/buffer/src/lib.rs @@ -20,7 +20,7 @@ use rpc::proto; pub use selection::*; use std::{ cmp, - convert::{TryFrom, TryInto}, + convert::TryFrom, iter::Iterator, ops::Range, str, @@ -486,20 +486,8 @@ impl Buffer { .selections .into_iter() .map(|set| { - let set_id = clock::Lamport { - replica_id: set.replica_id as ReplicaId, - value: set.lamport_timestamp, - }; - let selections: Vec = set - .selections - .into_iter() - .map(TryFrom::try_from) - .collect::>()?; - let set = SelectionSet { - selections: Arc::from(selections), - active: set.is_active, - }; - Result::<_, anyhow::Error>::Ok((set_id, set)) + let set = SelectionSet::try_from(set)?; + Result::<_, anyhow::Error>::Ok((set.id, set)) }) .collect::>()?; Ok(buffer) @@ -859,6 +847,7 @@ impl Buffer { self.selections.insert( set_id, SelectionSet { + id: set_id, selections, active: false, }, @@ -1290,18 +1279,19 @@ impl Buffer { pub fn add_selection_set(&mut self, selections: impl Into>) -> Operation { let selections = selections.into(); - let lamport_timestamp = self.lamport_clock.tick(); + let set_id = self.lamport_clock.tick(); self.selections.insert( - lamport_timestamp, + set_id, SelectionSet { + id: set_id, selections: selections.clone(), active: false, }, ); Operation::UpdateSelections { - set_id: lamport_timestamp, + set_id, selections, - lamport_timestamp, + lamport_timestamp: set_id, } } @@ -2390,26 +2380,6 @@ impl TryFrom for Anchor { } } -impl TryFrom for Selection { - type Error = anyhow::Error; - - fn try_from(selection: proto::Selection) -> Result { - Ok(Selection { - id: selection.id as usize, - start: selection - .start - .ok_or_else(|| anyhow!("missing selection start"))? - .try_into()?, - end: selection - .end - .ok_or_else(|| anyhow!("missing selection end"))? - .try_into()?, - reversed: selection.reversed, - goal: SelectionGoal::None, - }) - } -} - pub trait ToOffset { fn to_offset<'a>(&self, content: impl Into>) -> usize; } diff --git a/crates/buffer/src/selection.rs b/crates/buffer/src/selection.rs index df0834b67f..589746803b 100644 --- a/crates/buffer/src/selection.rs +++ b/crates/buffer/src/selection.rs @@ -1,7 +1,13 @@ -use rpc::proto; - use crate::{Anchor, Buffer, Point, ToOffset as _, ToPoint as _}; -use std::{cmp::Ordering, mem, ops::Range, sync::Arc}; +use anyhow::anyhow; +use rpc::proto; +use std::{ + cmp::Ordering, + convert::{TryFrom, TryInto}, + mem, + ops::Range, + sync::Arc, +}; pub type SelectionSetId = clock::Lamport; pub type SelectionsVersion = usize; @@ -24,8 +30,9 @@ pub struct Selection { #[derive(Clone, Debug, Eq, PartialEq)] pub struct SelectionSet { - pub selections: Arc<[Selection]>, + pub id: SelectionSetId, pub active: bool, + pub selections: Arc<[Selection]>, } impl Selection { @@ -92,3 +99,43 @@ impl<'a> Into for &'a Selection { } } } + +impl TryFrom for Selection { + type Error = anyhow::Error; + + fn try_from(selection: proto::Selection) -> Result { + Ok(Selection { + id: selection.id as usize, + start: selection + .start + .ok_or_else(|| anyhow!("missing selection start"))? + .try_into()?, + end: selection + .end + .ok_or_else(|| anyhow!("missing selection end"))? + .try_into()?, + reversed: selection.reversed, + goal: SelectionGoal::None, + }) + } +} + +impl TryFrom for SelectionSet { + type Error = anyhow::Error; + + fn try_from(set: proto::SelectionSet) -> Result { + Ok(Self { + id: clock::Lamport { + replica_id: set.replica_id as u16, + value: set.lamport_timestamp, + }, + active: set.is_active, + selections: Arc::from( + set.selections + .into_iter() + .map(TryInto::try_into) + .collect::, _>>()?, + ), + }) + } +}