Skip to content

Commit

Permalink
style: Run cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
manokara committed Nov 11, 2020
1 parent 3c162bd commit b9b7bec
Show file tree
Hide file tree
Showing 7 changed files with 337 additions and 156 deletions.
23 changes: 15 additions & 8 deletions benches/json.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use bencode::Value;
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use nanoserde::{DeJson, SerJson};

const DICT_MIXED: &'static str = "{\
Expand All @@ -14,7 +14,10 @@ const DICT_MIXED: &'static str = "{\
\"zyx\":[0,1,2]\
}";

fn val<T>(t: T) -> Value where T: Into<Value> {
fn val<T>(t: T) -> Value
where
T: Into<Value>,
{
t.into()
}

Expand All @@ -35,15 +38,19 @@ pub fn serialize(c: &mut Criterion) {
"zyx".into() => vec![Value::Int(0), Value::Int(1), Value::Int(2)].into(),
}));

c.bench_function("serialize", |b| b.iter(|| {
dict_mixed.serialize_json();
}));
c.bench_function("serialize", |b| {
b.iter(|| {
dict_mixed.serialize_json();
})
});
}

pub fn deserialize(c: &mut Criterion) {
c.bench_function("deserialize", |b| b.iter(|| {
Value::deserialize_json(black_box(DICT_MIXED)).expect("Couldn't deserialize JSON");
}));
c.bench_function("deserialize", |b| {
b.iter(|| {
Value::deserialize_json(black_box(DICT_MIXED)).expect("Couldn't deserialize JSON");
})
});
}

criterion_group!(benches, serialize, deserialize);
Expand Down
22 changes: 14 additions & 8 deletions benches/load.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,31 @@
use std::fs::File;
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use std::fs::File;

const TORRENT_PATH: &'static str = concat!(env!("CARGO_MANIFEST_DIR"), "/data/torrent.benc");

pub fn load(c: &mut Criterion) {
let mut file = File::open(TORRENT_PATH).unwrap();

c.bench_function("load torrent file", |b| b.iter(|| {
bencode::load(black_box(&mut file)).expect("Failed loading file");
}));
c.bench_function("load torrent file", |b| {
b.iter(|| {
bencode::load(black_box(&mut file)).expect("Failed loading file");
})
});
}

pub fn encode(c: &mut Criterion) {
let mut file = File::open(TORRENT_PATH).unwrap();
let mut buffer = Vec::new();
let value = bencode::load(&mut file).expect("Failed loading file");

c.bench_function("encode value", |b| b.iter(|| {
value.encode(black_box(&mut buffer)).expect("Failed loading file");
buffer.clear();
}));
c.bench_function("encode value", |b| {
b.iter(|| {
value
.encode(black_box(&mut buffer))
.expect("Failed loading file");
buffer.clear();
})
});
}

criterion_group!(benches, load, encode);
Expand Down
15 changes: 7 additions & 8 deletions src/json.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use std::{
collections::BTreeMap,
str::Chars,
};
use nanoserde::{DeJson, DeJsonErr, DeJsonState, DeJsonTok, SerJson, SerJsonState};
use super::Value;
use nanoserde::{DeJson, DeJsonErr, DeJsonState, DeJsonTok, SerJson, SerJsonState};
use std::{collections::BTreeMap, str::Chars};

const INT_RANGE: &'static str = "0..=9223372036854775807";

Expand Down Expand Up @@ -53,7 +50,6 @@ impl DeJson for Value {
} else {
Err(state.err_range(INT_RANGE))
}

} else if let DeJsonTok::I64(i) = state.tok {
state.next_tok(input)?;
Ok(Value::Int(i))
Expand Down Expand Up @@ -109,7 +105,7 @@ impl SerJson for Value {
state.out.push(']');
}

Value::Bytes(v) => {
Value::Bytes(v) => {
let mut bytes = v.iter();

// TODO: Maybe do something more efficient than this?
Expand Down Expand Up @@ -155,7 +151,10 @@ mod tests {
\"zyx\":[0,1,2]\
}";

fn val<T>(t: T) -> Value where T: Into<Value> {
fn val<T>(t: T) -> Value
where
T: Into<Value>,
{
t.into()
}

Expand Down
17 changes: 6 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
#[cfg(feature = "json")]
mod json;
mod parser;
#[cfg(test)] mod tests;
#[cfg(feature = "json")] mod json;
#[cfg(test)]
mod tests;
mod value;

pub use parser::{
ParserError, Stream,
load, load_dict,
load_list, load_prim,
};
pub use parser::{load, load_dict, load_list, load_prim, ParserError, Stream};

pub use value::{
SelectError, TraverseError, TraverseAction,
Value, ValueAccessor, ValueDisplay,
};
pub use value::{SelectError, TraverseAction, TraverseError, Value, ValueAccessor, ValueDisplay};
Loading

0 comments on commit b9b7bec

Please sign in to comment.