Format with rustfmt (#389)

* Format with rustfmt

* Add rustfmt check to CI
This commit is contained in:
Taiki Endo
2020-05-22 13:17:30 +09:00
committed by GitHub
parent 5e93fa4c6b
commit 1fbf83816b
27 changed files with 410 additions and 331 deletions
+98 -97
View File
@@ -1,22 +1,23 @@
use core::{cmp, ptr, mem};
use core::{cmp, mem, ptr};
#[cfg(feature = "std")]
use std::io::IoSlice;
use alloc::{boxed::Box};
use alloc::boxed::Box;
macro_rules! buf_get_impl {
($this:ident, $typ:tt::$conv:tt) => ({
($this:ident, $typ:tt::$conv:tt) => {{
const SIZE: usize = mem::size_of::<$typ>();
// try to convert directly from the bytes
// this Option<ret> trick is to avoid keeping a borrow on self
// when advance() is called (mut borrow) and to call bytes() only once
let ret = $this.bytes().get(..SIZE).map(|src| unsafe {
$typ::$conv(*(src as *const _ as *const [_; SIZE]))
});
// try to convert directly from the bytes
// this Option<ret> trick is to avoid keeping a borrow on self
// when advance() is called (mut borrow) and to call bytes() only once
let ret = $this
.bytes()
.get(..SIZE)
.map(|src| unsafe { $typ::$conv(*(src as *const _ as *const [_; SIZE])) });
if let Some(ret) = ret {
// if the direct conversion was possible, advance and return
// if the direct conversion was possible, advance and return
$this.advance(SIZE);
return ret;
} else {
@@ -25,8 +26,8 @@ macro_rules! buf_get_impl {
$this.copy_to_slice(&mut buf); // (do the advance)
return $typ::$conv(buf);
}
});
(le => $this:ident, $typ:tt, $len_to_read:expr) => ({
}};
(le => $this:ident, $typ:tt, $len_to_read:expr) => {{
debug_assert!(mem::size_of::<$typ>() >= $len_to_read);
// The same trick as above does not improve the best case speed.
@@ -34,12 +35,12 @@ macro_rules! buf_get_impl {
let mut buf = [0; (mem::size_of::<$typ>())];
$this.copy_to_slice(&mut buf[..($len_to_read)]);
return $typ::from_le_bytes(buf);
});
}};
(be => $this:ident, $typ:tt, $len_to_read:expr) => {{
debug_assert!(mem::size_of::<$typ>() >= $len_to_read);
let mut buf = [0; (mem::size_of::<$typ>())];
$this.copy_to_slice(&mut buf[mem::size_of::<$typ>()-($len_to_read)..]);
$this.copy_to_slice(&mut buf[mem::size_of::<$typ>() - ($len_to_read)..]);
return $typ::from_be_bytes(buf);
}};
}
@@ -251,8 +252,7 @@ pub trait Buf {
let src = self.bytes();
cnt = cmp::min(src.len(), dst.len() - off);
ptr::copy_nonoverlapping(
src.as_ptr(), dst[off..].as_mut_ptr(), cnt);
ptr::copy_nonoverlapping(src.as_ptr(), dst[off..].as_mut_ptr(), cnt);
off += cnt;
}
@@ -810,109 +810,108 @@ pub trait Buf {
}
macro_rules! deref_forward_buf {
() => (
fn remaining(&self) -> usize {
(**self).remaining()
}
() => {
fn remaining(&self) -> usize {
(**self).remaining()
}
fn bytes(&self) -> &[u8] {
(**self).bytes()
}
fn bytes(&self) -> &[u8] {
(**self).bytes()
}
#[cfg(feature = "std")]
fn bytes_vectored<'b>(&'b self, dst: &mut [IoSlice<'b>]) -> usize {
(**self).bytes_vectored(dst)
}
#[cfg(feature = "std")]
fn bytes_vectored<'b>(&'b self, dst: &mut [IoSlice<'b>]) -> usize {
(**self).bytes_vectored(dst)
}
fn advance(&mut self, cnt: usize) {
(**self).advance(cnt)
}
fn advance(&mut self, cnt: usize) {
(**self).advance(cnt)
}
fn has_remaining(&self) -> bool {
(**self).has_remaining()
}
fn has_remaining(&self) -> bool {
(**self).has_remaining()
}
fn copy_to_slice(&mut self, dst: &mut [u8]) {
(**self).copy_to_slice(dst)
}
fn copy_to_slice(&mut self, dst: &mut [u8]) {
(**self).copy_to_slice(dst)
}
fn get_u8(&mut self) -> u8 {
(**self).get_u8()
}
fn get_u8(&mut self) -> u8 {
(**self).get_u8()
}
fn get_i8(&mut self) -> i8 {
(**self).get_i8()
}
fn get_i8(&mut self) -> i8 {
(**self).get_i8()
}
fn get_u16(&mut self) -> u16 {
(**self).get_u16()
}
fn get_u16(&mut self) -> u16 {
(**self).get_u16()
}
fn get_u16_le(&mut self) -> u16 {
(**self).get_u16_le()
}
fn get_u16_le(&mut self) -> u16 {
(**self).get_u16_le()
}
fn get_i16(&mut self) -> i16 {
(**self).get_i16()
}
fn get_i16(&mut self) -> i16 {
(**self).get_i16()
}
fn get_i16_le(&mut self) -> i16 {
(**self).get_i16_le()
}
fn get_i16_le(&mut self) -> i16 {
(**self).get_i16_le()
}
fn get_u32(&mut self) -> u32 {
(**self).get_u32()
}
fn get_u32(&mut self) -> u32 {
(**self).get_u32()
}
fn get_u32_le(&mut self) -> u32 {
(**self).get_u32_le()
}
fn get_u32_le(&mut self) -> u32 {
(**self).get_u32_le()
}
fn get_i32(&mut self) -> i32 {
(**self).get_i32()
}
fn get_i32(&mut self) -> i32 {
(**self).get_i32()
}
fn get_i32_le(&mut self) -> i32 {
(**self).get_i32_le()
}
fn get_i32_le(&mut self) -> i32 {
(**self).get_i32_le()
}
fn get_u64(&mut self) -> u64 {
(**self).get_u64()
}
fn get_u64(&mut self) -> u64 {
(**self).get_u64()
}
fn get_u64_le(&mut self) -> u64 {
(**self).get_u64_le()
}
fn get_u64_le(&mut self) -> u64 {
(**self).get_u64_le()
}
fn get_i64(&mut self) -> i64 {
(**self).get_i64()
}
fn get_i64(&mut self) -> i64 {
(**self).get_i64()
}
fn get_i64_le(&mut self) -> i64 {
(**self).get_i64_le()
}
fn get_i64_le(&mut self) -> i64 {
(**self).get_i64_le()
}
fn get_uint(&mut self, nbytes: usize) -> u64 {
(**self).get_uint(nbytes)
}
fn get_uint(&mut self, nbytes: usize) -> u64 {
(**self).get_uint(nbytes)
}
fn get_uint_le(&mut self, nbytes: usize) -> u64 {
(**self).get_uint_le(nbytes)
}
fn get_uint_le(&mut self, nbytes: usize) -> u64 {
(**self).get_uint_le(nbytes)
}
fn get_int(&mut self, nbytes: usize) -> i64 {
(**self).get_int(nbytes)
}
fn get_int(&mut self, nbytes: usize) -> i64 {
(**self).get_int(nbytes)
}
fn get_int_le(&mut self, nbytes: usize) -> i64 {
(**self).get_int_le(nbytes)
}
fn get_int_le(&mut self, nbytes: usize) -> i64 {
(**self).get_int_le(nbytes)
}
fn to_bytes(&mut self) -> crate::Bytes {
(**self).to_bytes()
}
)
fn to_bytes(&mut self) -> crate::Bytes {
(**self).to_bytes()
}
};
}
impl<T: Buf + ?Sized> Buf for &mut T {
@@ -950,7 +949,8 @@ impl Buf for Option<[u8; 1]> {
}
fn bytes(&self) -> &[u8] {
self.as_ref().map(AsRef::as_ref)
self.as_ref()
.map(AsRef::as_ref)
.unwrap_or(Default::default())
}
@@ -994,7 +994,8 @@ impl<T: AsRef<[u8]>> Buf for std::io::Cursor<T> {
fn advance(&mut self, cnt: usize) {
let pos = (self.position() as usize)
.checked_add(cnt).expect("overflow");
.checked_add(cnt)
.expect("overflow");
assert!(pos <= self.get_ref().as_ref().len());
self.set_position(pos as u64);
+89 -79
View File
@@ -1,9 +1,13 @@
use core::{cmp, mem::{self, MaybeUninit}, ptr, usize};
use core::{
cmp,
mem::{self, MaybeUninit},
ptr, usize,
};
#[cfg(feature = "std")]
use std::fmt;
use alloc::{vec::Vec, boxed::Box};
use alloc::{boxed::Box, vec::Vec};
/// A trait for values that provide sequential write access to bytes.
///
@@ -226,7 +230,10 @@ pub trait BufMut {
/// # Panics
///
/// Panics if `self` does not have enough capacity to contain `src`.
fn put<T: super::Buf>(&mut self, mut src: T) where Self: Sized {
fn put<T: super::Buf>(&mut self, mut src: T)
where
Self: Sized,
{
assert!(self.remaining_mut() >= src.remaining());
while src.has_remaining() {
@@ -237,14 +244,13 @@ pub trait BufMut {
let d = self.bytes_mut();
l = cmp::min(s.len(), d.len());
ptr::copy_nonoverlapping(
s.as_ptr(),
d.as_mut_ptr() as *mut u8,
l);
ptr::copy_nonoverlapping(s.as_ptr(), d.as_mut_ptr() as *mut u8, l);
}
src.advance(l);
unsafe { self.advance_mut(l); }
unsafe {
self.advance_mut(l);
}
}
}
@@ -270,7 +276,12 @@ pub trait BufMut {
fn put_slice(&mut self, src: &[u8]) {
let mut off = 0;
assert!(self.remaining_mut() >= src.len(), "buffer overflow; remaining = {}; src = {}", self.remaining_mut(), src.len());
assert!(
self.remaining_mut() >= src.len(),
"buffer overflow; remaining = {}; src = {}",
self.remaining_mut(),
src.len()
);
while off < src.len() {
let cnt;
@@ -279,16 +290,14 @@ pub trait BufMut {
let dst = self.bytes_mut();
cnt = cmp::min(dst.len(), src.len() - off);
ptr::copy_nonoverlapping(
src[off..].as_ptr(),
dst.as_mut_ptr() as *mut u8,
cnt);
ptr::copy_nonoverlapping(src[off..].as_ptr(), dst.as_mut_ptr() as *mut u8, cnt);
off += cnt;
}
unsafe { self.advance_mut(cnt); }
unsafe {
self.advance_mut(cnt);
}
}
}
@@ -872,84 +881,84 @@ pub trait BufMut {
}
macro_rules! deref_forward_bufmut {
() => (
fn remaining_mut(&self) -> usize {
(**self).remaining_mut()
}
() => {
fn remaining_mut(&self) -> usize {
(**self).remaining_mut()
}
fn bytes_mut(&mut self) -> &mut [MaybeUninit<u8>] {
(**self).bytes_mut()
}
fn bytes_mut(&mut self) -> &mut [MaybeUninit<u8>] {
(**self).bytes_mut()
}
#[cfg(feature = "std")]
fn bytes_vectored_mut<'b>(&'b mut self, dst: &mut [IoSliceMut<'b>]) -> usize {
(**self).bytes_vectored_mut(dst)
}
#[cfg(feature = "std")]
fn bytes_vectored_mut<'b>(&'b mut self, dst: &mut [IoSliceMut<'b>]) -> usize {
(**self).bytes_vectored_mut(dst)
}
unsafe fn advance_mut(&mut self, cnt: usize) {
(**self).advance_mut(cnt)
}
unsafe fn advance_mut(&mut self, cnt: usize) {
(**self).advance_mut(cnt)
}
fn put_slice(&mut self, src: &[u8]) {
(**self).put_slice(src)
}
fn put_slice(&mut self, src: &[u8]) {
(**self).put_slice(src)
}
fn put_u8(&mut self, n: u8) {
(**self).put_u8(n)
}
fn put_u8(&mut self, n: u8) {
(**self).put_u8(n)
}
fn put_i8(&mut self, n: i8) {
(**self).put_i8(n)
}
fn put_i8(&mut self, n: i8) {
(**self).put_i8(n)
}
fn put_u16(&mut self, n: u16) {
(**self).put_u16(n)
}
fn put_u16(&mut self, n: u16) {
(**self).put_u16(n)
}
fn put_u16_le(&mut self, n: u16) {
(**self).put_u16_le(n)
}
fn put_u16_le(&mut self, n: u16) {
(**self).put_u16_le(n)
}
fn put_i16(&mut self, n: i16) {
(**self).put_i16(n)
}
fn put_i16(&mut self, n: i16) {
(**self).put_i16(n)
}
fn put_i16_le(&mut self, n: i16) {
(**self).put_i16_le(n)
}
fn put_i16_le(&mut self, n: i16) {
(**self).put_i16_le(n)
}
fn put_u32(&mut self, n: u32) {
(**self).put_u32(n)
}
fn put_u32(&mut self, n: u32) {
(**self).put_u32(n)
}
fn put_u32_le(&mut self, n: u32) {
(**self).put_u32_le(n)
}
fn put_u32_le(&mut self, n: u32) {
(**self).put_u32_le(n)
}
fn put_i32(&mut self, n: i32) {
(**self).put_i32(n)
}
fn put_i32(&mut self, n: i32) {
(**self).put_i32(n)
}
fn put_i32_le(&mut self, n: i32) {
(**self).put_i32_le(n)
}
fn put_i32_le(&mut self, n: i32) {
(**self).put_i32_le(n)
}
fn put_u64(&mut self, n: u64) {
(**self).put_u64(n)
}
fn put_u64(&mut self, n: u64) {
(**self).put_u64(n)
}
fn put_u64_le(&mut self, n: u64) {
(**self).put_u64_le(n)
}
fn put_u64_le(&mut self, n: u64) {
(**self).put_u64_le(n)
}
fn put_i64(&mut self, n: i64) {
(**self).put_i64(n)
}
fn put_i64(&mut self, n: i64) {
(**self).put_i64(n)
}
fn put_i64_le(&mut self, n: i64) {
(**self).put_i64_le(n)
}
)
fn put_i64_le(&mut self, n: i64) {
(**self).put_i64_le(n)
}
};
}
impl<T: BufMut + ?Sized> BufMut for &mut T {
@@ -1013,15 +1022,16 @@ impl BufMut for Vec<u8> {
let len = self.len();
let ptr = self.as_mut_ptr() as *mut MaybeUninit<u8>;
unsafe {
&mut slice::from_raw_parts_mut(ptr, cap)[len..]
}
unsafe { &mut slice::from_raw_parts_mut(ptr, cap)[len..] }
}
// Specialize these methods so they can skip checking `remaining_mut`
// and `advance_mut`.
fn put<T: super::Buf>(&mut self, mut src: T) where Self: Sized {
fn put<T: super::Buf>(&mut self, mut src: T)
where
Self: Sized,
{
// In case the src isn't contiguous, reserve upfront
self.reserve(src.remaining());
+10 -11
View File
@@ -1,12 +1,12 @@
use crate::{Buf, BufMut};
use crate::buf::IntoIter;
use crate::{Buf, BufMut};
use core::mem::MaybeUninit;
#[cfg(feature = "std")]
use std::io::{IoSlice};
#[cfg(feature = "std")]
use crate::buf::IoSliceMut;
#[cfg(feature = "std")]
use std::io::IoSlice;
/// A `Chain` sequences two buffers.
///
@@ -41,10 +41,7 @@ pub struct Chain<T, U> {
impl<T, U> Chain<T, U> {
/// Creates a new `Chain` sequencing the provided values.
pub fn new(a: T, b: U) -> Chain<T, U> {
Chain {
a,
b,
}
Chain { a, b }
}
/// Gets a reference to the first underlying `Buf`.
@@ -137,8 +134,9 @@ impl<T, U> Chain<T, U> {
}
impl<T, U> Buf for Chain<T, U>
where T: Buf,
U: Buf,
where
T: Buf,
U: Buf,
{
fn remaining(&self) -> usize {
self.a.remaining() + self.b.remaining()
@@ -179,8 +177,9 @@ impl<T, U> Buf for Chain<T, U>
}
impl<T, U> BufMut for Chain<T, U>
where T: BufMut,
U: BufMut,
where
T: BufMut,
U: BufMut,
{
fn remaining_mut(&self) -> usize {
self.a.remaining_mut() + self.b.remaining_mut()
+1 -4
View File
@@ -11,10 +11,7 @@ pub struct Limit<T> {
}
pub(super) fn new<T>(inner: T, limit: usize) -> Limit<T> {
Limit {
inner,
limit,
}
Limit { inner, limit }
}
impl<T> Limit<T> {
+17 -7
View File
@@ -10,9 +10,9 @@ mod take;
#[cfg(feature = "std")]
mod writer;
pub use self::chain::Chain;
pub use self::limit::Limit;
pub use self::take::Take;
pub use self::chain::Chain;
#[cfg(feature = "std")]
pub use self::{reader::Reader, writer::Writer};
@@ -41,7 +41,8 @@ pub trait BufExt: Buf {
/// assert_eq!(dst, b" world");
/// ```
fn take(self, limit: usize) -> Take<Self>
where Self: Sized
where
Self: Sized,
{
take::new(self, limit)
}
@@ -62,7 +63,8 @@ pub trait BufExt: Buf {
/// assert_eq!(full.bytes(), b"hello world");
/// ```
fn chain<U: Buf>(self, next: U) -> Chain<Self, U>
where Self: Sized
where
Self: Sized,
{
Chain::new(self, next)
}
@@ -91,7 +93,10 @@ pub trait BufExt: Buf {
/// assert_eq!(&dst[..11], &b"hello world"[..]);
/// ```
#[cfg(feature = "std")]
fn reader(self) -> Reader<Self> where Self: Sized {
fn reader(self) -> Reader<Self>
where
Self: Sized,
{
reader::new(self)
}
}
@@ -114,7 +119,8 @@ pub trait BufMutExt: BufMut {
/// assert_eq!(dst.remaining_mut(), 10);
/// ```
fn limit(self, limit: usize) -> Limit<Self>
where Self: Sized
where
Self: Sized,
{
limit::new(self, limit)
}
@@ -142,7 +148,10 @@ pub trait BufMutExt: BufMut {
/// assert_eq!(*buf, b"hello world"[..]);
/// ```
#[cfg(feature = "std")]
fn writer(self) -> Writer<Self> where Self: Sized {
fn writer(self) -> Writer<Self>
where
Self: Sized,
{
writer::new(self)
}
@@ -167,7 +176,8 @@ pub trait BufMutExt: BufMut {
/// assert_eq!(&b[..], b" world");
/// ```
fn chain_mut<U: BufMut>(self, next: U) -> Chain<Self, U>
where Self: Sized
where
Self: Sized,
{
Chain::new(self, next)
}
+1 -1
View File
@@ -1,4 +1,4 @@
use crate::{Buf};
use crate::Buf;
use std::{cmp, io};
+1 -4
View File
@@ -13,10 +13,7 @@ pub struct Take<T> {
}
pub fn new<T>(inner: T, limit: usize) -> Take<T> {
Take {
inner,
limit,
}
Take { inner, limit }
}
impl<T> Take<T> {
+1 -2
View File
@@ -109,7 +109,6 @@ impl<T> IntoIter<T> {
}
}
impl<T: Buf> Iterator for IntoIter<T> {
type Item = u8;
@@ -130,4 +129,4 @@ impl<T: Buf> Iterator for IntoIter<T> {
}
}
impl<T: Buf> ExactSizeIterator for IntoIter<T> { }
impl<T: Buf> ExactSizeIterator for IntoIter<T> {}
+1 -2
View File
@@ -24,8 +24,7 @@ mod vec_deque;
pub use self::buf_impl::Buf;
pub use self::buf_mut::BufMut;
pub use self::ext::{BufExt, BufMutExt};
#[cfg(feature = "std")]
pub use self::buf_mut::IoSliceMut;
pub use self::ext::{BufExt, BufMutExt};
pub use self::iter::IntoIter;