forked from M-Labs/artiq
1
0
Fork 0

firmware: mark many functions in libio as #[inline].

This commit is contained in:
whitequark 2018-05-14 11:22:10 +00:00
parent 368d6f9bfb
commit 7e299563df
2 changed files with 26 additions and 1 deletions

View File

@ -105,27 +105,32 @@ pub struct Cursor<T> {
} }
impl<T> Cursor<T> { impl<T> Cursor<T> {
#[inline]
pub fn new(inner: T) -> Cursor<T> { pub fn new(inner: T) -> Cursor<T> {
Cursor { inner, pos: 0 } Cursor { inner, pos: 0 }
} }
#[inline]
pub fn into_inner(self) -> T { pub fn into_inner(self) -> T {
self.inner self.inner
} }
#[inline]
pub fn get_ref(&self) -> &T { pub fn get_ref(&self) -> &T {
&self.inner &self.inner
} }
#[inline]
pub fn get_mut(&mut self) -> &mut T { pub fn get_mut(&mut self) -> &mut T {
&mut self.inner &mut self.inner
} }
#[inline]
pub fn position(&self) -> usize { pub fn position(&self) -> usize {
self.pos self.pos
} }
#[inline]
pub fn set_position(&mut self, pos: usize) { pub fn set_position(&mut self, pos: usize) {
self.pos = pos self.pos = pos
} }
@ -155,6 +160,7 @@ impl<T: AsMut<[u8]>> Write for Cursor<T> {
Ok(len) Ok(len)
} }
#[inline]
fn flush(&mut self) -> result::Result<(), Self::FlushError> { fn flush(&mut self) -> result::Result<(), Self::FlushError> {
Ok(()) Ok(())
} }
@ -165,11 +171,13 @@ impl<T: ::alloc::Vec<[u8]>> Write for Cursor<T> {
type WriteError = !; type WriteError = !;
type FlushError = !; type FlushError = !;
#[inline]
fn write(&mut self, buf: &[u8]) -> result::Result<usize, Self::WriteError> { fn write(&mut self, buf: &[u8]) -> result::Result<usize, Self::WriteError> {
self.inner.extend(buf); self.inner.extend(buf);
Ok(buf.len()) Ok(buf.len())
} }
#[inline]
fn flush(&mut self) -> result::Result<(), Self::FlushError> { fn flush(&mut self) -> result::Result<(), Self::FlushError> {
Ok(()) Ok(())
} }

View File

@ -7,35 +7,41 @@ pub trait ProtoRead {
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Self::ReadError>; fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Self::ReadError>;
#[inline]
fn read_u8(&mut self) -> Result<u8, Self::ReadError> { fn read_u8(&mut self) -> Result<u8, Self::ReadError> {
let mut bytes = [0; 1]; let mut bytes = [0; 1];
self.read_exact(&mut bytes)?; self.read_exact(&mut bytes)?;
Ok(bytes[0]) Ok(bytes[0])
} }
#[inline]
fn read_u16(&mut self) -> Result<u16, Self::ReadError> { fn read_u16(&mut self) -> Result<u16, Self::ReadError> {
let mut bytes = [0; 2]; let mut bytes = [0; 2];
self.read_exact(&mut bytes)?; self.read_exact(&mut bytes)?;
Ok(NetworkEndian::read_u16(&bytes)) Ok(NetworkEndian::read_u16(&bytes))
} }
#[inline]
fn read_u32(&mut self) -> Result<u32, Self::ReadError> { fn read_u32(&mut self) -> Result<u32, Self::ReadError> {
let mut bytes = [0; 4]; let mut bytes = [0; 4];
self.read_exact(&mut bytes)?; self.read_exact(&mut bytes)?;
Ok(NetworkEndian::read_u32(&bytes)) Ok(NetworkEndian::read_u32(&bytes))
} }
#[inline]
fn read_u64(&mut self) -> Result<u64, Self::ReadError> { fn read_u64(&mut self) -> Result<u64, Self::ReadError> {
let mut bytes = [0; 8]; let mut bytes = [0; 8];
self.read_exact(&mut bytes)?; self.read_exact(&mut bytes)?;
Ok(NetworkEndian::read_u64(&bytes)) Ok(NetworkEndian::read_u64(&bytes))
} }
#[inline]
fn read_bool(&mut self) -> Result<bool, Self::ReadError> { fn read_bool(&mut self) -> Result<bool, Self::ReadError> {
Ok(self.read_u8()? != 0) Ok(self.read_u8()? != 0)
} }
#[cfg(feature = "alloc")] #[cfg(feature = "alloc")]
#[inline]
fn read_bytes(&mut self) -> Result<::alloc::Vec<u8>, Self::ReadError> { fn read_bytes(&mut self) -> Result<::alloc::Vec<u8>, Self::ReadError> {
let length = self.read_u32()?; let length = self.read_u32()?;
let mut value = vec![0; length as usize]; let mut value = vec![0; length as usize];
@ -55,61 +61,72 @@ pub trait ProtoWrite {
fn write_all(&mut self, buf: &[u8]) -> Result<(), Self::WriteError>; fn write_all(&mut self, buf: &[u8]) -> Result<(), Self::WriteError>;
#[inline]
fn write_u8(&mut self, value: u8) -> Result<(), Self::WriteError> { fn write_u8(&mut self, value: u8) -> Result<(), Self::WriteError> {
let bytes = [value; 1]; let bytes = [value; 1];
self.write_all(&bytes) self.write_all(&bytes)
} }
#[inline]
fn write_i8(&mut self, value: i8) -> Result<(), Self::WriteError> { fn write_i8(&mut self, value: i8) -> Result<(), Self::WriteError> {
let bytes = [value as u8; 1]; let bytes = [value as u8; 1];
self.write_all(&bytes) self.write_all(&bytes)
} }
#[inline]
fn write_u16(&mut self, value: u16) -> Result<(), Self::WriteError> { fn write_u16(&mut self, value: u16) -> Result<(), Self::WriteError> {
let mut bytes = [0; 2]; let mut bytes = [0; 2];
NetworkEndian::write_u16(&mut bytes, value); NetworkEndian::write_u16(&mut bytes, value);
self.write_all(&bytes) self.write_all(&bytes)
} }
#[inline]
fn write_i16(&mut self, value: i16) -> Result<(), Self::WriteError> { fn write_i16(&mut self, value: i16) -> Result<(), Self::WriteError> {
let mut bytes = [0; 2]; let mut bytes = [0; 2];
NetworkEndian::write_i16(&mut bytes, value); NetworkEndian::write_i16(&mut bytes, value);
self.write_all(&bytes) self.write_all(&bytes)
} }
#[inline]
fn write_u32(&mut self, value: u32) -> Result<(), Self::WriteError> { fn write_u32(&mut self, value: u32) -> Result<(), Self::WriteError> {
let mut bytes = [0; 4]; let mut bytes = [0; 4];
NetworkEndian::write_u32(&mut bytes, value); NetworkEndian::write_u32(&mut bytes, value);
self.write_all(&bytes) self.write_all(&bytes)
} }
#[inline]
fn write_i32(&mut self, value: i32) -> Result<(), Self::WriteError> { fn write_i32(&mut self, value: i32) -> Result<(), Self::WriteError> {
let mut bytes = [0; 4]; let mut bytes = [0; 4];
NetworkEndian::write_i32(&mut bytes, value); NetworkEndian::write_i32(&mut bytes, value);
self.write_all(&bytes) self.write_all(&bytes)
} }
#[inline]
fn write_u64(&mut self, value: u64) -> Result<(), Self::WriteError> { fn write_u64(&mut self, value: u64) -> Result<(), Self::WriteError> {
let mut bytes = [0; 8]; let mut bytes = [0; 8];
NetworkEndian::write_u64(&mut bytes, value); NetworkEndian::write_u64(&mut bytes, value);
self.write_all(&bytes) self.write_all(&bytes)
} }
#[inline]
fn write_i64(&mut self, value: i64) -> Result<(), Self::WriteError> { fn write_i64(&mut self, value: i64) -> Result<(), Self::WriteError> {
let mut bytes = [0; 8]; let mut bytes = [0; 8];
NetworkEndian::write_i64(&mut bytes, value); NetworkEndian::write_i64(&mut bytes, value);
self.write_all(&bytes) self.write_all(&bytes)
} }
#[inline]
fn write_bool(&mut self, value: bool) -> Result<(), Self::WriteError> { fn write_bool(&mut self, value: bool) -> Result<(), Self::WriteError> {
self.write_u8(value as u8) self.write_u8(value as u8)
} }
#[inline]
fn write_bytes(&mut self, value: &[u8]) -> Result<(), Self::WriteError> { fn write_bytes(&mut self, value: &[u8]) -> Result<(), Self::WriteError> {
self.write_u32(value.len() as u32)?; self.write_u32(value.len() as u32)?;
self.write_all(value) self.write_all(value)
} }
#[inline]
fn write_string(&mut self, value: &str) -> Result<(), Self::WriteError> { fn write_string(&mut self, value: &str) -> Result<(), Self::WriteError> {
self.write_bytes(value.as_bytes()) self.write_bytes(value.as_bytes())
} }