-
-
Notifications
You must be signed in to change notification settings - Fork 146
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature request: Add write_uXX_from to WriteBytesExt trait #155
Comments
I think there would be fine to add, yes. I believe the necessary APIs for implementing this are already available on the ByteOrder trait. |
@BurntSushi, #[inline]
fn write_u16_from<T: ByteOrder>(&mut self, src: &[u16]) -> Result<()> {
for &n in src {
self.write_u16::<T>(n)?
}
Ok(())
} The problem here is that we can get an error after some data is already written. In this case we can do nothing to roll back the writer to last stable state. |
If that's the only implementation choice, then we probably shouldn't provide it. |
You can do a bit better if you check if It would look something a bit like pub trait ByteOrderIo: ByteOrder {
fn write_u16_from<W: Write + ?Sized>(writer: &mut W, src: &[u16]) -> Result<()>;
// ...
}
impl ByteOrderIo for LittleEndian {
#[cfg(target_endian = "little")]
fn write_u16_from<W: Write + ?Sized>(writer: &mut W, src: &[u16]) -> Result<()> {
let base = src.as_ptr() as *const u8;
// SAFETY: I don't think len can overflow here, since creating a slice at all
// requires len * size_of <= isize::MAX, but maybe do some splitting into parts
// if that's a worry.
let casted_slice = unsafe { std::slice::from_raw_parts(base, src.len() * std::mem::size_of::<u16>()) };
writer.write_all(casted_slice)
}
#[cfg(not(target_endian = "little"))]
pub fn write_u16_from<W: Write + ?Sized>(writer: &mut W, src: &[u16]) -> Result<()> {
// But we could also batch calls to write_all here via using a larger buffer.
let mut buf = [0u8; 2];
for &n in src {
Self::write_u16(&mut buf, n);
writer.write_all(&buf)?;
}
Ok(())
}
} |
I write some kind of protocol parser and I use next code to read protocol data:
I would like to use the same logic for writer:
Is that already possible? Or it needs implementation?
The text was updated successfully, but these errors were encountered: