Skip to content

Commit

Permalink
Merge pull request #439 from khuey/request_structs
Browse files Browse the repository at this point in the history
Generate and use structs to serialize requests.
  • Loading branch information
mergify[bot] authored May 24, 2020
2 parents b43fda0 + 6885036 commit b371d89
Show file tree
Hide file tree
Showing 33 changed files with 34,828 additions and 18,794 deletions.
602 changes: 349 additions & 253 deletions generator/src/generator/namespace.rs

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
//! | Get | `Cookie::reply` | `Cookie::reply_unchecked` |
//! | Ignore | `Cookie::discard_reply_and_errors` | Just drop the cookie |
use std::borrow::Cow;
use std::convert::{TryFrom, TryInto};
use std::io::IoSlice;

Expand All @@ -63,6 +64,7 @@ pub type SequenceNumber = u64;
pub type BufWithFds<B> = (B, Vec<RawFdContainer>);
pub type EventAndSeqNumber = (Event, SequenceNumber);
pub type RawEventAndSeqNumber<B> = (B, SequenceNumber);
pub type PiecewiseBuf<'a> = Vec<Cow<'a, [u8]>>;

/// Either a raw reply or a raw error response to an X11 request.
#[derive(Debug)]
Expand Down
44 changes: 29 additions & 15 deletions src/protocol/bigreq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use std::io::IoSlice;
use crate::utils::RawFdContainer;
#[allow(unused_imports)]
use crate::x11_utils::{Serialize, TryParse};
use crate::connection::RequestConnection;
use crate::connection::{BufWithFds, PiecewiseBuf, RequestConnection};
#[allow(unused_imports)]
use crate::cookie::{Cookie, CookieWithFds, VoidCookie};
use crate::errors::{ConnectionError, ParseError};
Expand All @@ -34,24 +34,38 @@ pub const X11_XML_VERSION: (u32, u32) = (0, 0);

/// Opcode for the Enable request
pub const ENABLE_REQUEST: u8 = 0;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct EnableRequest;
impl EnableRequest {
/// Serialize this request into bytes for the provided connection
fn serialize<'input, Conn>(self, conn: &Conn) -> Result<BufWithFds<PiecewiseBuf<'input>>, ConnectionError>
where
Conn: RequestConnection + ?Sized,
{
let extension_information = conn.extension_information(X11_EXTENSION_NAME)?
.ok_or(ConnectionError::UnsupportedExtension)?;
let length_so_far = 0;
let mut request0 = vec![
extension_information.major_opcode,
ENABLE_REQUEST,
0,
0,
];
let length_so_far = length_so_far + request0.len();
assert_eq!(length_so_far % 4, 0);
let length = u16::try_from(length_so_far / 4).unwrap_or(0);
request0[2..4].copy_from_slice(&length.to_ne_bytes());
Ok((vec![request0.into()], vec![]))
}
}
pub fn enable<Conn>(conn: &Conn) -> Result<Cookie<'_, Conn, EnableReply>, ConnectionError>
where
Conn: RequestConnection + ?Sized,
{
let extension_information = conn.extension_information(X11_EXTENSION_NAME)?
.ok_or(ConnectionError::UnsupportedExtension)?;
let length_so_far = 0;
let mut request0 = [
extension_information.major_opcode,
ENABLE_REQUEST,
0,
0,
];
let length_so_far = length_so_far + request0.len();
assert_eq!(length_so_far % 4, 0);
let length = u16::try_from(length_so_far / 4).unwrap_or(0);
request0[2..4].copy_from_slice(&length.to_ne_bytes());
Ok(conn.send_request_with_reply(&[IoSlice::new(&request0)], vec![])?)
let request0 = EnableRequest;
let (bytes, fds) = request0.serialize(conn)?;
let slices = bytes.iter().map(|b| IoSlice::new(&*b)).collect::<Vec<_>>();
Ok(conn.send_request_with_reply(&slices, fds)?)
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
Expand Down
Loading

0 comments on commit b371d89

Please # to comment.