psychon / x11rb

X11 bindings for the rust programming language, similar to xcb being the X11 C bindings
Apache License 2.0
366 stars 40 forks source link

Use &mut [IoSlice] for request sending #427

Open psychon opened 4 years ago

psychon commented 4 years ago

XCBConnection creates a copy of the passed in slice of request buffers. So far, RustConnection does not, but instead switches between write_vectored and plain write for a buffer: https://github.com/psychon/x11rb/blob/314242b7fcdee3f3603010ca22c79f89793d5629/src/rust_connection/mod.rs#L682-L683

I think it should be possible to get rid of that write part by using a mutable slice for request sending. That way, partial writes can be done by replacing the respective IoSlice.

psychon commented 4 years ago

Changing the public API from &[] to &mut [] is easy. Using this change in RustConnection::write_all_vectored then runs into lifetime problems. Oh and compute_length_field also ended up with lifetime problems. Someone who understands covariance and invariance may give this a shot.

Part of the problem is that I do not see how to implement https://doc.rust-lang.org/std/io/struct.IoSlice.html#method.advance on stable without unsafe code. When you have a &'b IoSlice<'a>, you can only get an &'b [u8], but would need an &'a [u8] to implement advance().

notgull commented 2 years ago

I created the advance crate as an "unsafe-quarantine microcrate" to allow for usage of the advance function in stable environments. Using that, we could probably resolve this issue.

eduardosm commented 3 months ago

IoSlice::advance will be stabilized in Rust 1.81