fix: better try_recv mutex logic for cursor ctrl

This commit is contained in:
əlemi 2024-08-13 18:05:46 +02:00
parent 2a016a6619
commit ab9c6530cd
Signed by: alemi
GPG key ID: A4895B84D311642C

View file

@ -67,8 +67,9 @@ impl Controller<Cursor> for CursorController {
/// try to receive without blocking, but will still block on stream mutex /// try to receive without blocking, but will still block on stream mutex
fn try_recv(&self) -> crate::Result<Option<Cursor>> { fn try_recv(&self) -> crate::Result<Option<Cursor>> {
let mut stream = self.0.stream.blocking_lock(); match self.0.stream.try_lock() {
match stream.try_recv() { Err(_) => Ok(None),
Ok(mut stream) => match stream.try_recv() {
Ok(x) => Ok(Some(x.into())), Ok(x) => Ok(Some(x.into())),
Err(TryRecvError::Empty) => Ok(None), Err(TryRecvError::Empty) => Ok(None),
Err(TryRecvError::Closed) => Err(crate::Error::Channel { send: false }), Err(TryRecvError::Closed) => Err(crate::Error::Channel { send: false }),
@ -78,6 +79,7 @@ impl Controller<Cursor> for CursorController {
} }
} }
} }
}
// TODO is this cancelable? so it can be used in tokio::select! // TODO is this cancelable? so it can be used in tokio::select!
// TODO is the result type overkill? should be an option? // TODO is the result type overkill? should be an option?