Skip to content

Commit

Permalink
Merge pull request #259 from eduardosm/buffer
Browse files Browse the repository at this point in the history
Get rid of `Buffer` and use an associated type in the `RequestConnection` trait
  • Loading branch information
psychon authored Mar 7, 2020
2 parents cd5cbe9 + 8a1f4f5 commit e7ed7bc
Show file tree
Hide file tree
Showing 20 changed files with 290 additions and 301 deletions.
16 changes: 8 additions & 8 deletions code_generator_helpers/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -613,33 +613,33 @@ def error(self, error, name):
self.out("")

def _emit_from_generic(self, name, from_generic_type, extra_name):
self.out("impl From<%s> for %s%s {", from_generic_type, self._name(name), extra_name)
self.out("impl<B: AsRef<[u8]>> From<%s<B>> for %s%s {", from_generic_type, self._name(name), extra_name)
with Indent(self.out):
self.out("fn from(value: %s) -> Self {", from_generic_type)
self.out("fn from(value: %s<B>) -> Self {", from_generic_type)
self.out.indent("Self::try_from(value.raw_bytes())" +
".expect(\"Buffer should be large enough so that parsing cannot fail\")")
self.out("}")
self.out("}")
self.out("impl From<&%s> for %s%s {", from_generic_type, self._name(name), extra_name)
self.out("impl<B: AsRef<[u8]>> From<&%s<B>> for %s%s {", from_generic_type, self._name(name), extra_name)
with Indent(self.out):
self.out("fn from(value: &%s) -> Self {", from_generic_type)
self.out("fn from(value: &%s<B>) -> Self {", from_generic_type)
self.out.indent("Self::try_from(value.raw_bytes())" +
".expect(\"Buffer should be large enough so that parsing cannot fail\")")
self.out("}")
self.out("}")

def _emit_tryfrom_generic(self, name, from_generic_type, extra_name):
self.out("impl TryFrom<%s> for %s%s {", from_generic_type, self._name(name), extra_name)
self.out("impl<B: AsRef<[u8]>> TryFrom<%s<B>> for %s%s {", from_generic_type, self._name(name), extra_name)
with Indent(self.out):
self.out("type Error = ParseError;")
self.out("fn try_from(value: %s) -> Result<Self, Self::Error> {", from_generic_type)
self.out("fn try_from(value: %s<B>) -> Result<Self, Self::Error> {", from_generic_type)
self.out.indent("Self::try_from(value.raw_bytes())")
self.out("}")
self.out("}")
self.out("impl TryFrom<&%s> for %s%s {", from_generic_type, self._name(name), extra_name)
self.out("impl<B: AsRef<[u8]>> TryFrom<&%s<B>> for %s%s {", from_generic_type, self._name(name), extra_name)
with Indent(self.out):
self.out("type Error = ParseError;")
self.out("fn try_from(value: &%s) -> Result<Self, Self::Error> {", from_generic_type)
self.out("fn try_from(value: &%s<B>) -> Result<Self, Self::Error> {", from_generic_type)
self.out.indent("Self::try_from(value.raw_bytes())")
self.out("}")
self.out("}")
Expand Down
3 changes: 1 addition & 2 deletions examples/check_unchecked_requests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@
extern crate x11rb;

use x11rb::connection::Connection;
use x11rb::errors::ReplyError;
use x11rb::generated::xproto::ConnectionExt;
use x11rb::wrapper::ConnectionExt as _;
use x11rb::x11_utils::Event;

const INVALID_WINDOW: u32 = 0;

fn main() -> Result<(), ReplyError> {
fn main() -> Result<(), Box<dyn std::error::Error>> {
let (conn, _) = x11rb::connect(None).unwrap();

// For requests with responses, there are four possibilities:
Expand Down
4 changes: 2 additions & 2 deletions examples/hypnomoire.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ fn run<C: Connection>(
screen_num: usize,
white: GCONTEXT,
black: GCONTEXT,
) -> Result<(), ReplyOrIdError> {
) -> Result<(), ReplyOrIdError<C::Buf>> {
let screen = &conn.setup().roots[screen_num];
let default_size = 300;
let pixmap = conn.generate_id()?;
Expand Down Expand Up @@ -180,7 +180,7 @@ fn event_thread<C>(
conn_arc: Arc<C>,
windows: Vec<Arc<Mutex<Window>>>,
white: GCONTEXT,
) -> Result<(), ReplyError>
) -> Result<(), ReplyError<C::Buf>>
where
C: Connection + Send + Sync + 'static,
{
Expand Down
14 changes: 9 additions & 5 deletions examples/shared_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl<C: Connection> Drop for FreePixmap<'_, C> {
}

/// Get the supported SHM version from the X11 server
fn check_shm_version<C: Connection>(conn: &C) -> Result<Option<(u16, u16)>, ReplyError> {
fn check_shm_version<C: Connection>(conn: &C) -> Result<Option<(u16, u16)>, ReplyError<C::Buf>> {
if conn
.extension_information(shm::X11_EXTENSION_NAME)?
.is_none()
Expand All @@ -42,7 +42,7 @@ fn get_shared_memory_content_at_offset<C: Connection>(
screen: &xproto::Screen,
shmseg: shm::SEG,
offset: u32,
) -> Result<Vec<u8>, ReplyOrIdError> {
) -> Result<Vec<u8>, ReplyOrIdError<C::Buf>> {
let width = match screen.root_depth {
24 => 1,
16 => 2,
Expand Down Expand Up @@ -70,7 +70,7 @@ fn use_shared_mem<C: Connection>(
conn: &C,
screen_num: usize,
shmseg: shm::SEG,
) -> Result<(), ReplyOrIdError> {
) -> Result<(), ReplyOrIdError<C::Buf>> {
let screen = &conn.setup().roots[screen_num];

let content = get_shared_memory_content_at_offset(conn, screen, shmseg, 0)?;
Expand All @@ -96,7 +96,11 @@ fn make_file() -> IOResult<File> {
Ok(file)
}

fn send_fd<C: Connection>(conn: &C, screen_num: usize, file: File) -> Result<(), ReplyOrIdError> {
fn send_fd<C: Connection>(
conn: &C,
screen_num: usize,
file: File,
) -> Result<(), ReplyOrIdError<C::Buf>> {
let shmseg = conn.generate_id()?;
conn.shm_attach_fd(shmseg, file, false)?;

Expand All @@ -107,7 +111,7 @@ fn send_fd<C: Connection>(conn: &C, screen_num: usize, file: File) -> Result<(),
Ok(())
}

fn receive_fd<C: Connection>(conn: &C, screen_num: usize) -> Result<(), ReplyOrIdError> {
fn receive_fd<C: Connection>(conn: &C, screen_num: usize) -> Result<(), ReplyOrIdError<C::Buf>> {
let shmseg = conn.generate_id()?;
let segment_size = TEMP_FILE_CONTENT.len() as _;
let reply = conn
Expand Down
32 changes: 19 additions & 13 deletions examples/simple_window_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ struct WMState<'a, C: Connection> {
}

impl<'a, C: Connection> WMState<'a, C> {
fn new(conn: &'a C, screen_num: usize) -> Result<WMState<'a, C>, ReplyOrIdError> {
fn new(conn: &'a C, screen_num: usize) -> Result<WMState<'a, C>, ReplyOrIdError<C::Buf>> {
let screen = &conn.setup().roots[screen_num];
let black_gc = conn.generate_id()?;
let font = conn.generate_id()?;
Expand Down Expand Up @@ -85,7 +85,7 @@ impl<'a, C: Connection> WMState<'a, C> {
}

/// Scan for already existing windows and manage them
fn scan_windows(&mut self) -> Result<(), ReplyOrIdError> {
fn scan_windows(&mut self) -> Result<(), ReplyOrIdError<C::Buf>> {
// Get the already existing top-level windows.
let screen = &self.conn.setup().roots[self.screen_num];
let tree_reply = self.conn.query_tree(screen.root)?.reply()?;
Expand Down Expand Up @@ -118,7 +118,7 @@ impl<'a, C: Connection> WMState<'a, C> {
&mut self,
win: WINDOW,
geom: &GetGeometryReply,
) -> Result<(), ReplyOrIdError> {
) -> Result<(), ReplyOrIdError<C::Buf>> {
println!("Managing window {:?}", win);
let screen = &self.conn.setup().roots[self.screen_num];
assert!(self.find_window_by_id(win).is_none());
Expand Down Expand Up @@ -153,7 +153,7 @@ impl<'a, C: Connection> WMState<'a, C> {
}

/// Draw the titlebar of a window
fn redraw_titlebar(&self, state: &WindowState) -> Result<(), ReplyError> {
fn redraw_titlebar(&self, state: &WindowState) -> Result<(), ReplyError<C::Buf>> {
let close_x = state.close_x_position();
self.conn.poly_line(
CoordMode::Origin,
Expand Down Expand Up @@ -199,7 +199,7 @@ impl<'a, C: Connection> WMState<'a, C> {
}

/// Do all pending work that was queued while handling some events
fn refresh(&mut self) -> Result<(), ReplyError> {
fn refresh(&mut self) -> Result<(), ReplyError<C::Buf>> {
while let Some(&win) = self.pending_expose.iter().next() {
self.pending_expose.remove(&win);
if let Some(state) = self.find_window_by_id(win) {
Expand Down Expand Up @@ -227,7 +227,7 @@ impl<'a, C: Connection> WMState<'a, C> {
}

/// Handle the given event
fn handle_event(&mut self, event: GenericEvent) -> Result<(), ReplyOrIdError> {
fn handle_event(&mut self, event: GenericEvent<C::Buf>) -> Result<(), ReplyOrIdError<C::Buf>> {
println!("Got event {:?}", event);
match event.response_type() {
UNMAP_NOTIFY_EVENT => self.handle_unmap_notify(event.into())?,
Expand All @@ -241,7 +241,7 @@ impl<'a, C: Connection> WMState<'a, C> {
Ok(())
}

fn handle_unmap_notify(&mut self, event: UnmapNotifyEvent) -> Result<(), ReplyError> {
fn handle_unmap_notify(&mut self, event: UnmapNotifyEvent) -> Result<(), ReplyError<C::Buf>> {
let conn = self.conn;
self.windows.retain(|state| {
if state.window != event.window {
Expand All @@ -253,7 +253,10 @@ impl<'a, C: Connection> WMState<'a, C> {
Ok(())
}

fn handle_configure_request(&mut self, event: ConfigureRequestEvent) -> Result<(), ReplyError> {
fn handle_configure_request(
&mut self,
event: ConfigureRequestEvent,
) -> Result<(), ReplyError<C::Buf>> {
if let Some(state) = self.find_window_by_id_mut(event.window) {
let _ = state;
unimplemented!();
Expand All @@ -276,19 +279,19 @@ impl<'a, C: Connection> WMState<'a, C> {
Ok(())
}

fn handle_map_request(&mut self, event: MapRequestEvent) -> Result<(), ReplyOrIdError> {
fn handle_map_request(&mut self, event: MapRequestEvent) -> Result<(), ReplyOrIdError<C::Buf>> {
self.manage_window(
event.window,
&self.conn.get_geometry(event.window)?.reply()?,
)
}

fn handle_expose(&mut self, event: ExposeEvent) -> Result<(), ReplyError> {
fn handle_expose(&mut self, event: ExposeEvent) -> Result<(), ReplyError<C::Buf>> {
self.pending_expose.insert(event.window);
Ok(())
}

fn handle_enter(&mut self, event: EnterNotifyEvent) -> Result<(), ReplyError> {
fn handle_enter(&mut self, event: EnterNotifyEvent) -> Result<(), ReplyError<C::Buf>> {
let window = if let Some(state) = self.find_window_by_id(event.child) {
state.window
} else {
Expand All @@ -298,7 +301,10 @@ impl<'a, C: Connection> WMState<'a, C> {
Ok(())
}

fn handle_button_release(&mut self, event: ButtonReleaseEvent) -> Result<(), ReplyError> {
fn handle_button_release(
&mut self,
event: ButtonReleaseEvent,
) -> Result<(), ReplyError<C::Buf>> {
if let Some(state) = self.find_window_by_id(event.event) {
let data = [self.wm_delete_window, 0, 0, 0, 0];
let event = ClientMessageEvent {
Expand All @@ -316,7 +322,7 @@ impl<'a, C: Connection> WMState<'a, C> {
}
}

fn become_wm<C: Connection>(conn: &C, screen: &Screen) -> Result<(), ReplyError> {
fn become_wm<C: Connection>(conn: &C, screen: &Screen) -> Result<(), ReplyError<C::Buf>> {
// Try to become the window manager. This causes an error if there is already another WM.
let change = ChangeWindowAttributesAux::default().event_mask(
EventMask::SubstructureRedirect | EventMask::SubstructureNotify | EventMask::EnterWindow,
Expand Down
32 changes: 16 additions & 16 deletions examples/tutorial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1511,7 +1511,7 @@ fn gc_font_get<C: Connection>(
screen: &Screen,
window: WINDOW,
font_name: &str,
) -> Result<GCONTEXT, ReplyOrIdError> {
) -> Result<GCONTEXT, ReplyOrIdError<C::Buf>> {
let font = conn.generate_id()?;

conn.open_font(font, font_name.as_bytes())?;
Expand Down Expand Up @@ -1765,7 +1765,7 @@ fn example9() -> Result<(), Box<dyn Error>> {
// be done like this:

#[allow(unused)]
fn example_move<C: Connection>(conn: &C, win: WINDOW) -> Result<(), ReplyError> {
fn example_move<C: Connection>(conn: &C, win: WINDOW) -> Result<(), ReplyError<C::Buf>> {
// Move the window to coordinates x = 10 and y = 20
let values = ConfigureWindowAux::default().x(10).y(20);
conn.configure_window(win, &values)?;
Expand All @@ -1783,7 +1783,7 @@ fn example_move<C: Connection>(conn: &C, win: WINDOW) -> Result<(), ReplyError>
// following code:

#[allow(unused)]
fn example_resize<C: Connection>(conn: &C, win: WINDOW) -> Result<(), ReplyError> {
fn example_resize<C: Connection>(conn: &C, win: WINDOW) -> Result<(), ReplyError<C::Buf>> {
// Move the window to coordinates width = 10 and height = 20
let values = ConfigureWindowAux::default().width(10).height(20);
conn.configure_window(win, &values)?;
Expand All @@ -1794,7 +1794,7 @@ fn example_resize<C: Connection>(conn: &C, win: WINDOW) -> Result<(), ReplyError
// `xcb_configure_window_t`:

#[allow(unused)]
fn example_move_resize<C: Connection>(conn: &C, win: WINDOW) -> Result<(), ReplyError> {
fn example_move_resize<C: Connection>(conn: &C, win: WINDOW) -> Result<(), ReplyError<C::Buf>> {
// Move the window to coordinates x = 10 and y = 20
// and resize the window to width = 200 and height = 300
let values = ConfigureWindowAux::default()
Expand All @@ -1816,15 +1816,15 @@ fn example_move_resize<C: Connection>(conn: &C, win: WINDOW) -> Result<(), Reply
// manipulate our windows stack order:

#[allow(unused)]
fn example_stack_above<C: Connection>(conn: &C, win: WINDOW) -> Result<(), ReplyError> {
fn example_stack_above<C: Connection>(conn: &C, win: WINDOW) -> Result<(), ReplyError<C::Buf>> {
// Move the window on the top of the stack
let values = ConfigureWindowAux::default().stack_mode(StackMode::Above);
conn.configure_window(win, &values)?;
Ok(())
}

#[allow(unused)]
fn example_stack_below<C: Connection>(conn: &C, win: WINDOW) -> Result<(), ReplyError> {
fn example_stack_below<C: Connection>(conn: &C, win: WINDOW) -> Result<(), ReplyError<C::Buf>> {
// Move the window to the bottom of the stack
let values = ConfigureWindowAux::default().stack_mode(StackMode::Below);
conn.configure_window(win, &values)?;
Expand Down Expand Up @@ -1858,7 +1858,7 @@ pub struct RenamedGetGeometryReply {
// You use them as follows:

#[allow(unused)]
fn example_get_geometry<C: Connection>(conn: &C, win: WINDOW) -> Result<(), ReplyError> {
fn example_get_geometry<C: Connection>(conn: &C, win: WINDOW) -> Result<(), ReplyError<C::Buf>> {
let geom = conn.get_geometry(win)?.reply()?;

// Do something with the fields of geom
Expand Down Expand Up @@ -1910,7 +1910,7 @@ fn example_get_geometry<C: Connection>(conn: &C, win: WINDOW) -> Result<(), Repl
// We use them as follows:

#[allow(unused)]
fn example_get_and_query<C: Connection>(conn: &C, win: WINDOW) -> Result<(), ReplyError> {
fn example_get_and_query<C: Connection>(conn: &C, win: WINDOW) -> Result<(), ReplyError<C::Buf>> {
let geom = conn.get_geometry(win)?;
let tree = conn.query_tree(win)?;
let geom = geom.reply()?;
Expand Down Expand Up @@ -1957,7 +1957,7 @@ fn example_get_and_query<C: Connection>(conn: &C, win: WINDOW) -> Result<(), Rep
// You use them as follows:

#[allow(unused)]
fn example_get_attributes<C: Connection>(conn: &C, win: WINDOW) -> Result<(), ReplyError> {
fn example_get_attributes<C: Connection>(conn: &C, win: WINDOW) -> Result<(), ReplyError<C::Buf>> {
let geom = conn.get_window_attributes(win)?.reply()?;

// Do something with the fields of attr
Expand Down Expand Up @@ -2051,7 +2051,7 @@ fn example_create_colormap<C: Connection>(
conn: &C,
win: WINDOW,
screen: &Screen,
) -> Result<(), ReplyOrIdError> {
) -> Result<(), ReplyOrIdError<C::Buf>> {
let cmap = conn.generate_id()?;
conn.create_colormap(ColormapAlloc::None, cmap, win, screen.root_visual)?;

Expand Down Expand Up @@ -2093,7 +2093,7 @@ fn example_fill_colormap<C: Connection>(
conn: &C,
win: WINDOW,
screen: &Screen,
) -> Result<(), ReplyOrIdError> {
) -> Result<(), ReplyOrIdError<C::Buf>> {
let cmap = conn.generate_id()?;
conn.create_colormap(ColormapAlloc::None, cmap, win, screen.root_visual)?;
let _rep = conn.alloc_color(cmap, 65535, 0, 0)?.reply()?;
Expand Down Expand Up @@ -2246,7 +2246,7 @@ fn example_create_glyph_cursor<C: Connection>(
conn: &C,
win: WINDOW,
screen: &Screen,
) -> Result<(), ReplyOrIdError> {
) -> Result<(), ReplyOrIdError<C::Buf>> {
let font = conn.generate_id()?;
conn.open_font(font, b"cursor")?;

Expand Down Expand Up @@ -2282,7 +2282,7 @@ fn example_change_window_cursor<C: Connection>(
conn: &C,
win: WINDOW,
cursor: CURSOR,
) -> Result<(), ReplyError> {
) -> Result<(), ReplyError<C::Buf>> {
let values = ChangeWindowAttributesAux::default().cursor(cursor);
conn.change_window_attributes(win, &values)?;

Expand All @@ -2307,7 +2307,7 @@ fn button_draw<C: Connection>(
x1: i16,
y1: i16,
label: &str,
) -> Result<(), ReplyOrIdError> {
) -> Result<(), ReplyOrIdError<C::Buf>> {
let inset = 2;
let gc = gc_font_get(conn, screen, window, "7x13")?;
let width = 7 * label.len() + 2 * (inset + 1);
Expand Down Expand Up @@ -2344,7 +2344,7 @@ fn cursor_set<C: Connection>(
screen: &Screen,
window: WINDOW,
cursor_id: u16,
) -> Result<(), ReplyOrIdError> {
) -> Result<(), ReplyOrIdError<C::Buf>> {
let font = conn.generate_id()?;
conn.open_font(font, b"cursor")?;

Expand Down Expand Up @@ -2716,7 +2716,7 @@ fn example_get_visual2<C: Connection>(conn: &C, screen_num: usize) {
fn example_create_default_gc<C: Connection>(
conn: &C,
screen_num: usize,
) -> Result<GCONTEXT, ReplyOrIdError> {
) -> Result<GCONTEXT, ReplyOrIdError<C::Buf>> {
let screen = &conn.setup().roots[screen_num];
let values = CreateGCAux::default()
.foreground(screen.black_pixel)
Expand Down
Loading

0 comments on commit e7ed7bc

Please sign in to comment.