Skip to content

Commit

Permalink
Cookie: Replace TryFrom with TryParse
Browse files Browse the repository at this point in the history
Instead of requiring replies to implement TryFrom<&[u8],
Error=ParseError>, this commit changes the code to instead use the
TryParse trait that we have just for this purpose.

Related-to: #478
Signed-off-by: Uli Schlachter <[email protected]>
  • Loading branch information
psychon committed Dec 31, 2020
1 parent 0aa9f2b commit 5c81da0
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 15 deletions.
6 changes: 3 additions & 3 deletions src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ use crate::errors::{ConnectionError, ParseError, ReplyError, ReplyOrIdError};
use crate::protocol::xproto::Setup;
use crate::protocol::Event;
use crate::utils::RawFdContainer;
use crate::x11_utils::{ExtensionInformation, X11Error};
use crate::x11_utils::{ExtensionInformation, TryParse, X11Error};

/// Number type used for referring to things that were sent to the server in responses from the
/// server.
Expand Down Expand Up @@ -119,7 +119,7 @@ pub trait RequestConnection {
fds: Vec<RawFdContainer>,
) -> Result<Cookie<'_, Self, R>, ConnectionError>
where
R: for<'a> TryFrom<&'a [u8], Error = ParseError>;
R: TryParse;

/// Send a request with a reply containing file descriptors to the server.
///
Expand Down Expand Up @@ -496,7 +496,7 @@ pub enum DiscardMode {
///
/// fn send_request_with_reply<R>(&self, bufs: &[IoSlice], fds: Vec<RawFdContainer>)
/// -> Result<Cookie<Self, R>, ConnectionError>
/// where R: for<'a> TryFrom<&'a [u8], Error=ParseError> {
/// where R: TryParse {
/// Ok(Cookie::new(self, self.send_request(bufs, fds, true, false)?))
/// }
///
Expand Down
9 changes: 5 additions & 4 deletions src/cookie.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Cookies are handles to future replies or errors from the X11 server.
use std::convert::{TryFrom, TryInto};
use std::convert::TryFrom;
use std::marker::PhantomData;

use crate::connection::{BufWithFds, DiscardMode, RequestConnection, RequestKind, SequenceNumber};
Expand All @@ -9,6 +9,7 @@ use crate::errors::{ConnectionError, ParseError, ReplyError};
use crate::protocol::record::EnableContextReply;
use crate::protocol::xproto::ListFontsWithInfoReply;
use crate::utils::RawFdContainer;
use crate::x11_utils::TryParse;

/// A handle to a possible error from the X11 server.
///
Expand Down Expand Up @@ -144,7 +145,7 @@ where

impl<C, R> Cookie<'_, C, R>
where
R: for<'a> TryFrom<&'a [u8], Error = ParseError>,
R: TryParse,
C: RequestConnection + ?Sized,
{
/// Construct a new cookie.
Expand Down Expand Up @@ -177,13 +178,13 @@ where

/// Get the reply that the server sent.
pub fn reply(self) -> Result<R, ReplyError> {
Ok(self.raw_reply()?.as_ref().try_into()?)
Ok(R::try_parse(self.raw_reply()?.as_ref())?.0)
}

/// Get the reply that the server sent, but have errors handled as events.
pub fn reply_unchecked(self) -> Result<Option<R>, ConnectionError> {
self.raw_reply_unchecked()?
.map(|buf| buf.as_ref().try_into())
.map(|buf| R::try_parse(buf.as_ref()).map(|r| r.0))
.transpose()
.map_err(Into::into)
}
Expand Down
4 changes: 2 additions & 2 deletions src/extension_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ mod test {
use crate::cookie::{Cookie, CookieWithFds, VoidCookie};
use crate::errors::{ConnectionError, ParseError};
use crate::utils::RawFdContainer;
use crate::x11_utils::{ExtInfoProvider, ExtensionInformation};
use crate::x11_utils::{ExtInfoProvider, ExtensionInformation, TryParse};

use super::{CheckState, ExtensionManager};

Expand All @@ -173,7 +173,7 @@ mod test {
_fds: Vec<RawFdContainer>,
) -> Result<Cookie<'_, Self, R>, ConnectionError>
where
R: for<'a> TryFrom<&'a [u8], Error = ParseError>,
R: TryParse,
{
Ok(Cookie::new(self, 1))
}
Expand Down
4 changes: 2 additions & 2 deletions src/rust_connection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::extension_manager::ExtensionManager;
use crate::protocol::bigreq::{ConnectionExt as _, EnableReply};
use crate::protocol::xproto::{Setup, SetupRequest, GET_INPUT_FOCUS_REQUEST};
use crate::utils::RawFdContainer;
use crate::x11_utils::{ExtensionInformation, Serialize};
use crate::x11_utils::{ExtensionInformation, Serialize, TryParse};

mod id_allocator;
mod inner;
Expand Down Expand Up @@ -451,7 +451,7 @@ impl<S: Stream> RequestConnection for RustConnection<S> {
fds: Vec<RawFdContainer>,
) -> Result<Cookie<'_, Self, Reply>, ConnectionError>
where
Reply: for<'a> TryFrom<&'a [u8], Error = ParseError>,
Reply: TryParse,
{
Ok(Cookie::new(
self,
Expand Down
4 changes: 2 additions & 2 deletions src/xcb_ffi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub use crate::errors::{ConnectError, ConnectionError, ParseError, ReplyError, R
use crate::extension_manager::ExtensionManager;
use crate::protocol::xproto::Setup;
use crate::utils::{CSlice, RawFdContainer};
use crate::x11_utils::ExtensionInformation;
use crate::x11_utils::{ExtensionInformation, TryParse};

mod pending_errors;
mod raw_ffi;
Expand Down Expand Up @@ -356,7 +356,7 @@ impl RequestConnection for XCBConnection {
fds: Vec<RawFdContainer>,
) -> Result<Cookie<'_, Self, R>, ConnectionError>
where
R: for<'a> TryFrom<&'a [u8], Error = ParseError>,
R: TryParse,
{
Ok(Cookie::new(
self,
Expand Down
2 changes: 1 addition & 1 deletion tests/regression_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ impl RequestConnection for FakeConnection {
fds: Vec<RawFdContainer>,
) -> Result<Cookie<Self, R>, ConnectionError>
where
R: for<'a> TryFrom<&'a [u8], Error = ParseError>,
R: TryParse,
{
Ok(Cookie::new(self, self.internal_send_request(bufs, fds)?))
}
Expand Down
2 changes: 1 addition & 1 deletion tests/resource_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ mod test {
_: Vec<RawFdContainer>,
) -> Result<Cookie<'_, Self, R>, ConnectionError>
where
R: for<'a> TryFrom<&'a [u8], Error = ParseError>,
R: TryParse,
{
Ok(Cookie::new(self, 42))
}
Expand Down

0 comments on commit 5c81da0

Please sign in to comment.