From 365811d9637acb14f43a5522646ed75fb509c156 Mon Sep 17 00:00:00 2001 From: alemi Date: Sat, 1 Jul 2023 20:22:39 +0200 Subject: [PATCH] chore: thanks clippy --- client/nvim/src/main.rs | 24 ++++++++++++------------ server/src/buffer/service.rs | 22 ++++++---------------- src/cursor.rs | 14 ++++++++++---- src/operation/controller.rs | 4 ++-- src/operation/mod.rs | 6 ++---- 5 files changed, 32 insertions(+), 38 deletions(-) diff --git a/client/nvim/src/main.rs b/client/nvim/src/main.rs index e7976b4..2acc73b 100644 --- a/client/nvim/src/main.rs +++ b/client/nvim/src/main.rs @@ -19,19 +19,19 @@ struct NeovimHandler { factories: Arc>>>, } -fn nullable_optional_str(args: &Vec, index: usize) -> Option { +fn nullable_optional_str(args: &[Value], index: usize) -> Option { Some(args.get(index)?.as_str()?.to_string()) } -fn default_empty_str(args: &Vec, index: usize) -> String { +fn default_empty_str(args: &[Value], index: usize) -> String { nullable_optional_str(args, index).unwrap_or("".into()) } -fn nullable_optional_number(args: &Vec, index: usize) -> Option { - Some(args.get(index)?.as_i64()?) +fn nullable_optional_number(args: &[Value], index: usize) -> Option { + args.get(index)?.as_i64() } -fn default_zero_number(args: &Vec, index: usize) -> i64 { +fn default_zero_number(args: &[Value], index: usize) -> i64 { nullable_optional_number(args, index).unwrap_or(0) } @@ -56,7 +56,7 @@ impl Handler for NeovimHandler { "ping" => Ok(Value::from("pong")), "create" => { - if args.len() < 1 { + if args.is_empty() { return Err(Value::from("no path given")); } let path = default_empty_str(&args, 0); @@ -77,7 +77,7 @@ impl Handler for NeovimHandler { } let path = default_empty_str(&args, 0); let txt = default_empty_str(&args, 1); - let mut pos = default_zero_number(&args, 2) as i64; + let mut pos = default_zero_number(&args, 2); if pos <= 0 { pos = 0 } // TODO wtf vim?? @@ -126,7 +126,7 @@ impl Handler for NeovimHandler { }, "attach" => { - if args.len() < 1 { + if args.is_empty() { return Err(Value::from("no path given")); } let path = default_empty_str(&args, 0); @@ -141,7 +141,7 @@ impl Handler for NeovimHandler { Err(e) => Err(Value::from(format!("could not attach to stream: {}", e))), Ok(controller) => { let _controller = controller.clone(); - let lines : Vec = _controller.content().split("\n").map(|x| x.to_string()).collect(); + let lines : Vec = _controller.content().split('\n').map(|x| x.to_string()).collect(); match buffer.set_lines(0, -1, false, lines).await { Err(e) => Err(Value::from(format!("could not sync buffer: {}", e))), Ok(()) => { @@ -150,7 +150,7 @@ impl Handler for NeovimHandler { if !_controller.run() { break debug!("buffer updater clean exit") } let _span = _controller.wait().await; // TODO only change lines affected! - let lines : Vec = _controller.content().split("\n").map(|x| x.to_string()).collect(); + let lines : Vec = _controller.content().split('\n').map(|x| x.to_string()).collect(); if let Err(e) = buffer.set_lines(0, -1, false, lines).await { error!("could not update buffer: {}", e); } @@ -165,7 +165,7 @@ impl Handler for NeovimHandler { }, "detach" => { - if args.len() < 1 { + if args.is_empty() { return Err(Value::from("no path given")); } let path = default_empty_str(&args, 0); @@ -176,7 +176,7 @@ impl Handler for NeovimHandler { }, "listen" => { - if args.len() < 1 { + if args.is_empty() { return Err(Value::from("no path given")); } let path = default_empty_str(&args, 0); diff --git a/server/src/buffer/service.rs b/server/src/buffer/service.rs index 87e20fd..76fb3b8 100644 --- a/server/src/buffer/service.rs +++ b/server/src/buffer/service.rs @@ -61,14 +61,9 @@ impl Buffer for BufferService { let (tx, rx) = mpsc::channel(128); let mut sub = handle.subscribe(); tokio::spawn(async move { - loop { - match sub.recv().await { - Ok(v) => { - if v.user == myself { continue } - tx.send(Ok(v)).await.unwrap(); // TODO unnecessary channel? - } - Err(_e) => break, - } + while let Ok(v) = sub.recv().await { + if v.user == myself { continue } + tx.send(Ok(v)).await.unwrap(); // TODO unnecessary channel? } }); let output_stream = ReceiverStream::new(rx); @@ -83,14 +78,9 @@ impl Buffer for BufferService { let myself = req.into_inner().user; let (tx, rx) = mpsc::channel(128); tokio::spawn(async move { - loop { - match sub.recv().await { - Ok(v) => { - if v.user == myself { continue } - tx.send(Ok(v)).await.unwrap(); // TODO unnecessary channel? - } - Err(_e) => break, - } + while let Ok(v) = sub.recv().await { + if v.user == myself { continue } + tx.send(Ok(v)).await.unwrap(); // TODO unnecessary channel? } }); let output_stream = ReceiverStream::new(rx); diff --git a/src/cursor.rs b/src/cursor.rs index 371e296..97ca69a 100644 --- a/src/cursor.rs +++ b/src/cursor.rs @@ -7,7 +7,7 @@ use crate::proto::CursorMov; /// Note that this differs from any hashmap in its put method: no &mut! pub trait CursorStorage { - fn get(&self, id: &String) -> Option; + fn get(&self, id: &str) -> Option; fn put(&self, id: String, val: Cursor); fn update(&self, event: CursorMov) -> Option { @@ -44,8 +44,8 @@ pub struct CursorController { _bus_keepalive: Mutex>, } -impl CursorController { - pub fn new() -> Self { +impl Default for CursorController { + fn default() -> Self { let (tx, _rx) = broadcast::channel(64); CursorController { users: Mutex::new(HashMap::new()), @@ -53,6 +53,12 @@ impl CursorController { _bus_keepalive: Mutex::new(_rx), } } +} + +impl CursorController { + pub fn new() -> Self { + CursorController::default() + } pub fn sub(&self) -> broadcast::Receiver<(String, Cursor)> { self.bus.subscribe() @@ -77,7 +83,7 @@ impl CursorStorage for CursorController { Some(cur) } - fn get(&self, id: &String) -> Option { + fn get(&self, id: &str) -> Option { Some(self.users.lock().unwrap().get(id)?.clone()) } diff --git a/src/operation/controller.rs b/src/operation/controller.rs index 5cc16e9..d2764c3 100644 --- a/src/operation/controller.rs +++ b/src/operation/controller.rs @@ -46,7 +46,7 @@ impl OperationController { pub async fn poll(&self) -> Option { let len = self.queue.lock().unwrap().len(); - if len <= 0 { + if len == 0 { let mut recv = self.last.lock().unwrap().clone(); // TODO less jank way recv.changed().await.unwrap_or_warn("wairing for op changes #1"); // acknowledge current state @@ -80,7 +80,7 @@ impl OperationController { async fn operation(&self, op: &OperationSeq) -> Result, OTError> { let txt = self.content(); let res = op.apply(&txt)?; - *self.text.lock().unwrap() = res.clone(); + *self.text.lock().unwrap() = res; Ok(op_effective_range(op)) } } diff --git a/src/operation/mod.rs b/src/operation/mod.rs index 6ed85bc..5890e23 100644 --- a/src/operation/mod.rs +++ b/src/operation/mod.rs @@ -15,10 +15,8 @@ pub const fn tailing_noop(seq: &[Operation]) -> u64 { count_noop(seq.last()) } const fn count_noop(op: Option<&Operation>) -> u64 { match op { None => 0, - Some(op) => match op { - Operation::Retain(n) => *n, - _ => 0, - } + Some(Operation::Retain(n)) => *n, + Some(_) => 0, } }