Skip to content

Commit

Permalink
Reformat everything with cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
Frago9876543210 committed Jul 26, 2021
1 parent 19fa68d commit 1443321
Show file tree
Hide file tree
Showing 11 changed files with 51 additions and 38 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 13,8 @@ endiannezz = "0.6"
```
### Using `#[derive(Io)]`
```rust
use std::io::Result;
use endiannezz::Io;
use std::io::Result;

#[derive(Io)]
#[endian(big)]
Expand All @@ -37,6 37,7 @@ fn main() -> Result<()> {
s1.write(&mut vec)?;

let mut slice = vec.as_slice();
#[rustfmt::skip]
assert_eq!(slice, &[
1, //bool as byte
0, 0, 0, 10, //u32 in big-endian (because big-endian is set on top place struct as default)
Expand All @@ -52,8 53,9 @@ fn main() -> Result<()> {

### Simple example
```rust
use endiannezz::ext::{EndianReader, EndianWriter};
use endiannezz::{BigEndian, LittleEndian, NativeEndian};
use std::io::Result;
use endiannezz::{NativeEndian, LittleEndian, BigEndian, ext::{EndianReader, EndianWriter}};

fn main() -> Result<()> {
let mut vec = Vec::new();
Expand Down
3 changes: 1 addition & 2 deletions derive/src/attr/endian.rs
Original file line number Diff line number Diff line change
@@ -1,8 1,7 @@
use crate::attr;
use proc_macro2::{Ident, Span};
use syn::{Attribute, Error, Meta, NestedMeta, Result};

use crate::attr;

macro_rules! ident {
($t:tt) => {
Ident::new(stringify!($t), Span::call_site())
Expand Down
6 changes: 2 additions & 4 deletions derive/src/fields.rs
Original file line number Diff line number Diff line change
@@ -1,9 1,7 @@
use crate::attr::endian;
use proc_macro2::{Ident, TokenStream};
use syn::{Fields, Result, Type};

use quote::{format_ident, quote};

use crate::attr::endian;
use syn::{Fields, Result, Type};

pub fn write<Named, Unnamed>(
fields: &Fields,
Expand Down
6 changes: 2 additions & 4 deletions derive/src/io.rs
Original file line number Diff line number Diff line change
@@ -1,9 1,7 @@
use crate::{attr, fields};
use proc_macro2::{Ident, Literal, TokenStream};
use syn::{Data, DeriveInput, Error, Fields, Result};

use quote::quote;

use crate::{attr, fields};
use syn::{Data, DeriveInput, Error, Fields, Result};

pub fn derive(input: DeriveInput) -> Result<TokenStream> {
let name = &input.ident;
Expand Down
1 change: 0 additions & 1 deletion derive/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 1,4 @@
use proc_macro::TokenStream;

use syn::{parse_macro_input, DeriveInput};

mod attr;
Expand Down
19 changes: 9 additions & 10 deletions src/ext.rs
Original file line number Diff line number Diff line change
@@ -1,8 1,7 @@
use crate::{Endian, Primitive};
use std::io::{Read, Result, Write};

use crate::{BigEndian, Endian, LittleEndian, NativeEndian, Primitive};

/// Allows to write primitive types with differents representation of bytes
/// Allows to write primitive types with different representation of bytes
pub trait EndianWriter: Write {
#[inline]
fn try_write<E: Endian, T: Primitive>(&mut self, primitive: T) -> Result<()> {
Expand All @@ -11,23 10,23 @@ pub trait EndianWriter: Write {

#[inline]
fn write_ne<T: Primitive>(&mut self, primitive: T) -> Result<()> {
self.try_write::<NativeEndian, T>(primitive)
self.try_write::<crate::NativeEndian, T>(primitive)
}

#[inline]
fn write_le<T: Primitive>(&mut self, primitive: T) -> Result<()> {
self.try_write::<LittleEndian, T>(primitive)
self.try_write::<crate::LittleEndian, T>(primitive)
}

#[inline]
fn write_be<T: Primitive>(&mut self, primitive: T) -> Result<()> {
self.try_write::<BigEndian, T>(primitive)
self.try_write::<crate::BigEndian, T>(primitive)
}
}

impl<W: Write ?Sized> EndianWriter for W {}

/// Allows to read primitive types with differents representation of bytes
/// Allows to read primitive types with different representation of bytes
pub trait EndianReader: Read {
#[inline]
fn try_read<E: Endian, T: Primitive>(&mut self) -> Result<T> {
Expand All @@ -36,17 35,17 @@ pub trait EndianReader: Read {

#[inline]
fn read_ne<T: Primitive>(&mut self) -> Result<T> {
self.try_read::<NativeEndian, T>()
self.try_read::<crate::NativeEndian, T>()
}

#[inline]
fn read_le<T: Primitive>(&mut self) -> Result<T> {
self.try_read::<LittleEndian, T>()
self.try_read::<crate::LittleEndian, T>()
}

#[inline]
fn read_be<T: Primitive>(&mut self) -> Result<T> {
self.try_read::<BigEndian, T>()
self.try_read::<crate::BigEndian, T>()
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/internal.rs
Original file line number Diff line number Diff line change
@@ -1,6 1,5 @@
use std::io::{Read, Result, Write};

use crate::{Endian, Io, Primitive};
use std::io::{Read, Result, Write};

pub trait HackedPrimitive: Primitive {
#[cfg_attr(feature = "inline_primitives", inline)]
Expand Down
23 changes: 15 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 17,9 @@ Now it's possible to have traits that expand [`Read`] and [`Write`] with new met
# Simple example
```rust
use endiannezz::ext::{EndianReader, EndianWriter};
use endiannezz::{BigEndian, LittleEndian, NativeEndian};
use std::io::Result;
use endiannezz::{NativeEndian, LittleEndian, BigEndian, ext::{EndianReader, EndianWriter}};
fn main() -> Result<()> {
let mut vec = Vec::new();
Expand All @@ -39,8 40,8 @@ fn main() -> Result<()> {
You can also use this syntax:
```rust
use endiannezz::{BigEndian, Endian, LittleEndian};
use std::io::Result;
use endiannezz::{Endian, BigEndian, LittleEndian};
fn main() -> Result<()> {
let mut vec = Vec::new();
Expand All @@ -54,8 55,9 @@ fn main() -> Result<()> {
# Using `#[derive(Io)]` to describe complex binary formats
```rust
use endiannezz::ext::{EndianReader, EndianWriter};
use endiannezz::{Io, LittleEndian};
use std::io::{Read, Result, Write};
use endiannezz::{ext::{EndianReader, EndianWriter}, Io, LittleEndian};
struct Bytes(Vec<u8>);
Expand All @@ -77,19 79,24 @@ impl Io for Bytes {
}
#[derive(Io)]
#[endian(little)] //default endian for fields of struct (except custom impl, such as Bytes)
//default endian for fields of struct (except custom impl, such as Bytes)
#[endian(little)]
//There are 3 types of endianness and they can be written in the `#[endian]` attribute as follows:
// - NativeEndian: `_`, `ne`, `native`
// - LittleEndian: `le`, `little`
// - BigEndian: `be`, `big`
struct Message {
bytes: Bytes, //will read/write data as is (according to implementation)
distance: u16, //u16 in little-endian
//will read/write data as is (according to implementation)
bytes: Bytes,
//u16 in little-endian
distance: u16,
//f32 in big-endian, you can override default endian!
#[endian(big)]
delta: f32, //f32 in big-endian, you can override default endian!
delta: f32,
#[endian(native)] //machine byte order
//machine byte order
#[endian(native)]
machine_data: u32,
}
Expand Down
15 changes: 12 additions & 3 deletions tests/derive/enums/nightly/regular_enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 4,14 @@ use endiannezz::Io;
#[endian(little)]
#[repr(u32)]
enum Foo {
Bar { x: bool } = 0xc0ffee,
Baz { x: u32, #[endian(big)] y: i16 } = 0xdead,
Bar {
x: bool,
} = 0xc0ffee,
Baz {
x: u32,
#[endian(big)]
y: i16,
} = 0xdead,
}

#[test]
Expand All @@ -27,7 33,10 @@ fn regular_enum() {
e3.write(&mut vec).unwrap();

let mut slice = vec.as_slice();
assert_eq!(slice, &[0xad, 0xde, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x10]);
assert_eq!(
slice,
&[0xad, 0xde, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x10]
);

let e4 = Foo::read(&mut slice).unwrap();
assert_eq!(e3, e4);
Expand Down
5 changes: 4 additions & 1 deletion tests/derive/enums/nightly/tuple_enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 27,10 @@ fn tuple_enum() {
e3.write(&mut vec).unwrap();

let mut slice = vec.as_slice();
assert_eq!(slice, &[0xad, 0xde, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x20]);
assert_eq!(
slice,
&[0xad, 0xde, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x20]
);

let e4 = Foo::read(&mut slice).unwrap();
assert_eq!(e3, e4);
Expand Down
2 changes: 1 addition & 1 deletion tests/derive/structs/header_struct.rs
Original file line number Diff line number Diff line change
@@ -1,4 1,4 @@
use endiannezz::{Io, HardcodedPayload};
use endiannezz::{HardcodedPayload, Io};

#[derive(Debug, Default, PartialEq)]
struct Header;
Expand Down

0 comments on commit 1443321

Please sign in to comment.