mirror of https://git.sr.ht/~stygianentity/bincode
Made all encode and decode functions always require a Config, removed _with_config functions
This commit is contained in:
parent
dd7e8e8e43
commit
707d0d238f
|
|
@ -10,11 +10,11 @@ fn slice_varint_u8(c: &mut Criterion) {
|
|||
.take(10_000)
|
||||
.collect();
|
||||
let config = Configuration::standard();
|
||||
let bytes = bincode::encode_to_vec_with_config(&input, config).unwrap();
|
||||
let bytes = bincode::encode_to_vec(&input, config).unwrap();
|
||||
|
||||
c.bench_function("slice_varint_u8", |b| {
|
||||
b.iter(|| {
|
||||
let _: Vec<u8> = bincode::decode_with_config(&bytes, config).unwrap();
|
||||
let _: Vec<u8> = bincode::decode_from_slice(&bytes, config).unwrap();
|
||||
})
|
||||
});
|
||||
}
|
||||
|
|
@ -26,11 +26,11 @@ fn slice_varint_u16(c: &mut Criterion) {
|
|||
.take(10_000)
|
||||
.collect();
|
||||
let config = Configuration::standard();
|
||||
let bytes = bincode::encode_to_vec_with_config(&input, config).unwrap();
|
||||
let bytes = bincode::encode_to_vec(&input, config).unwrap();
|
||||
|
||||
c.bench_function("slice_varint_u16", |b| {
|
||||
b.iter(|| {
|
||||
let _: Vec<u16> = bincode::decode_with_config(&bytes, config).unwrap();
|
||||
let _: Vec<u16> = bincode::decode_from_slice(&bytes, config).unwrap();
|
||||
})
|
||||
});
|
||||
}
|
||||
|
|
@ -42,11 +42,11 @@ fn slice_varint_u32(c: &mut Criterion) {
|
|||
.take(10_000)
|
||||
.collect();
|
||||
let config = Configuration::standard();
|
||||
let bytes = bincode::encode_to_vec_with_config(&input, config).unwrap();
|
||||
let bytes = bincode::encode_to_vec(&input, config).unwrap();
|
||||
|
||||
c.bench_function("slice_varint_u32", |b| {
|
||||
b.iter(|| {
|
||||
let _: Vec<u32> = bincode::decode_with_config(&bytes, config).unwrap();
|
||||
let _: Vec<u32> = bincode::decode_from_slice(&bytes, config).unwrap();
|
||||
})
|
||||
});
|
||||
}
|
||||
|
|
@ -58,11 +58,11 @@ fn slice_varint_u64(c: &mut Criterion) {
|
|||
.take(10_000)
|
||||
.collect();
|
||||
let config = Configuration::standard();
|
||||
let bytes = bincode::encode_to_vec_with_config(&input, config).unwrap();
|
||||
let bytes = bincode::encode_to_vec(&input, config).unwrap();
|
||||
|
||||
c.bench_function("slice_varint_u64", |b| {
|
||||
b.iter(|| {
|
||||
let _: Vec<u64> = bincode::decode_with_config(&bytes, config).unwrap();
|
||||
let _: Vec<u64> = bincode::decode_from_slice(&bytes, config).unwrap();
|
||||
})
|
||||
});
|
||||
}
|
||||
|
|
@ -74,12 +74,12 @@ fn bufreader_varint_u8(c: &mut Criterion) {
|
|||
.take(10_000)
|
||||
.collect();
|
||||
let config = Configuration::standard();
|
||||
let bytes = bincode::encode_to_vec_with_config(&input, config).unwrap();
|
||||
let bytes = bincode::encode_to_vec(&input, config).unwrap();
|
||||
|
||||
c.bench_function("bufreader_varint_u8", |b| {
|
||||
b.iter(|| {
|
||||
let _: Vec<u8> =
|
||||
bincode::decode_from_with_config(&mut std::io::BufReader::new(&bytes[..]), config)
|
||||
bincode::decode_from_reader(&mut std::io::BufReader::new(&bytes[..]), config)
|
||||
.unwrap();
|
||||
})
|
||||
});
|
||||
|
|
@ -92,12 +92,12 @@ fn bufreader_varint_u16(c: &mut Criterion) {
|
|||
.take(10_000)
|
||||
.collect();
|
||||
let config = Configuration::standard();
|
||||
let bytes = bincode::encode_to_vec_with_config(&input, config).unwrap();
|
||||
let bytes = bincode::encode_to_vec(&input, config).unwrap();
|
||||
|
||||
c.bench_function("bufreader_varint_u16", |b| {
|
||||
b.iter(|| {
|
||||
let _: Vec<u16> =
|
||||
bincode::decode_from_with_config(&mut std::io::BufReader::new(&bytes[..]), config)
|
||||
bincode::decode_from_reader(&mut std::io::BufReader::new(&bytes[..]), config)
|
||||
.unwrap();
|
||||
})
|
||||
});
|
||||
|
|
@ -110,12 +110,12 @@ fn bufreader_varint_u32(c: &mut Criterion) {
|
|||
.take(10_000)
|
||||
.collect();
|
||||
let config = Configuration::standard();
|
||||
let bytes = bincode::encode_to_vec_with_config(&input, config).unwrap();
|
||||
let bytes = bincode::encode_to_vec(&input, config).unwrap();
|
||||
|
||||
c.bench_function("bufreader_varint_u32", |b| {
|
||||
b.iter(|| {
|
||||
let _: Vec<u32> =
|
||||
bincode::decode_from_with_config(&mut std::io::BufReader::new(&bytes[..]), config)
|
||||
bincode::decode_from_reader(&mut std::io::BufReader::new(&bytes[..]), config)
|
||||
.unwrap();
|
||||
})
|
||||
});
|
||||
|
|
@ -128,12 +128,12 @@ fn bufreader_varint_u64(c: &mut Criterion) {
|
|||
.take(10_000)
|
||||
.collect();
|
||||
let config = Configuration::standard();
|
||||
let bytes = bincode::encode_to_vec_with_config(&input, config).unwrap();
|
||||
let bytes = bincode::encode_to_vec(&input, config).unwrap();
|
||||
|
||||
c.bench_function("bufreader_varint_u64", |b| {
|
||||
b.iter(|| {
|
||||
let _: Vec<u64> =
|
||||
bincode::decode_from_with_config(&mut std::io::BufReader::new(&bytes[..]), config)
|
||||
bincode::decode_from_reader(&mut std::io::BufReader::new(&bytes[..]), config)
|
||||
.unwrap();
|
||||
})
|
||||
});
|
||||
|
|
|
|||
16
docs/spec.md
16
docs/spec.md
|
|
@ -21,7 +21,7 @@ All tuples have no additional bytes, and are encoded in their specified order, e
|
|||
use bincode::config::Configuration;
|
||||
|
||||
let tuple = (u32::min_value(), i32::max_value()); // 8 bytes
|
||||
let encoded = bincode::encode_to_vec_with_config(tuple, Configuration::legacy()).unwrap();
|
||||
let encoded = bincode::encode_to_vec(tuple, Configuration::legacy()).unwrap();
|
||||
assert_eq!(encoded.as_slice(), &[
|
||||
0, 0, 0, 0, // 4 bytes for first type: u32
|
||||
255, 255, 255, 127 // 4 bytes for second type: i32
|
||||
|
|
@ -69,21 +69,21 @@ pub enum SomeEnum {
|
|||
}
|
||||
|
||||
// SomeEnum::A
|
||||
let encoded = bincode::encode_to_vec_with_config(SomeEnum::A, Configuration::legacy()).unwrap();
|
||||
let encoded = bincode::encode_to_vec(SomeEnum::A, Configuration::legacy()).unwrap();
|
||||
assert_eq!(encoded.as_slice(), &[
|
||||
0, 0, 0, 0, // first variant, A
|
||||
// no extra bytes because A has no fields
|
||||
]);
|
||||
|
||||
// SomeEnum::B(0)
|
||||
let encoded = bincode::encode_to_vec_with_config(SomeEnum::B(0), Configuration::legacy()).unwrap();
|
||||
let encoded = bincode::encode_to_vec(SomeEnum::B(0), Configuration::legacy()).unwrap();
|
||||
assert_eq!(encoded.as_slice(), &[
|
||||
1, 0, 0, 0, // first variant, B
|
||||
0, 0, 0, 0 // B has 1 unnamed field, which is an u32, so 4 bytes
|
||||
]);
|
||||
|
||||
// SomeEnum::C { value: 0u32 }
|
||||
let encoded = bincode::encode_to_vec_with_config(SomeEnum::C { value: 0u32 }, Configuration::legacy()).unwrap();
|
||||
let encoded = bincode::encode_to_vec(SomeEnum::C { value: 0u32 }, Configuration::legacy()).unwrap();
|
||||
assert_eq!(encoded.as_slice(), &[
|
||||
2, 0, 0, 0, // first variant, C
|
||||
0, 0, 0, 0 // C has 1 named field which is a u32, so 4 bytes
|
||||
|
|
@ -104,7 +104,7 @@ let list = vec![
|
|||
2u8
|
||||
];
|
||||
|
||||
let encoded = bincode::encode_to_vec_with_config(list, Configuration::legacy()).unwrap();
|
||||
let encoded = bincode::encode_to_vec(list, Configuration::legacy()).unwrap();
|
||||
assert_eq!(encoded.as_slice(), &[
|
||||
3, 0, 0, 0, 0, 0, 0, 0, // length of 3u64
|
||||
0, // entry 0
|
||||
|
|
@ -124,7 +124,7 @@ use bincode::config::Configuration;
|
|||
|
||||
let str = "Hello"; // Could also be `String::new(...)`
|
||||
|
||||
let encoded = bincode::encode_to_vec_with_config(str, Configuration::legacy()).unwrap();
|
||||
let encoded = bincode::encode_to_vec(str, Configuration::legacy()).unwrap();
|
||||
assert_eq!(encoded.as_slice(), &[
|
||||
5, 0, 0, 0, 0, 0, 0, 0, // length of the string, 5 bytes
|
||||
b'H', b'e', b'l', b'l', b'o'
|
||||
|
|
@ -139,7 +139,7 @@ Arrays are encoded *with* a length by default.
|
|||
use bincode::config::Configuration;
|
||||
|
||||
let arr: [u8; 5] = [10, 20, 30, 40, 50];
|
||||
let encoded = bincode::encode_to_vec_with_config(arr, Configuration::legacy()).unwrap();
|
||||
let encoded = bincode::encode_to_vec(arr, Configuration::legacy()).unwrap();
|
||||
assert_eq!(encoded.as_slice(), &[
|
||||
5, 0, 0, 0, 0, 0, 0, 0, // The length, as a u64
|
||||
10, 20, 30, 40, 50, // the bytes
|
||||
|
|
@ -168,7 +168,7 @@ let arr: [Foo; 2] = [
|
|||
},
|
||||
];
|
||||
|
||||
let encoded = bincode::encode_to_vec_with_config(arr, Configuration::legacy()).unwrap();
|
||||
let encoded = bincode::encode_to_vec(arr, Configuration::legacy()).unwrap();
|
||||
assert_eq!(encoded.as_slice(), &[
|
||||
2, 0, 0, 0, 0, 0, 0, 0, // Length of the array
|
||||
10, 20, // First Foo
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
use crate::{
|
||||
config,
|
||||
de::{Decode, Decoder},
|
||||
enc::{self, Encode, Encoder},
|
||||
error::{DecodeError, EncodeError},
|
||||
|
|
@ -21,18 +20,11 @@ impl enc::write::Writer for VecWriter {
|
|||
}
|
||||
}
|
||||
|
||||
/// Encode the given value into a `Vec<u8>`.
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
|
||||
pub fn encode_to_vec<E: enc::Encode>(val: E) -> Result<Vec<u8>, EncodeError> {
|
||||
encode_to_vec_with_config(val, config::Configuration::standard())
|
||||
}
|
||||
|
||||
/// Encode the given value into a `Vec<u8>` with the given `Config`. See the [config] module for more information.
|
||||
///
|
||||
/// [config]: config/index.html
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
|
||||
pub fn encode_to_vec_with_config<E: enc::Encode, C: Config>(
|
||||
val: E,
|
||||
config: C,
|
||||
) -> Result<Vec<u8>, EncodeError> {
|
||||
pub fn encode_to_vec<E: enc::Encode, C: Config>(val: E, config: C) -> Result<Vec<u8>, EncodeError> {
|
||||
let writer = VecWriter::default();
|
||||
let mut encoder = enc::EncoderImpl::<_, C>::new(writer, config);
|
||||
val.encode(&mut encoder)?;
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
use crate::{
|
||||
config::{self, Config},
|
||||
config::Config,
|
||||
de::{read::Reader, BorrowDecode, BorrowDecoder, Decode, Decoder, DecoderImpl},
|
||||
enc::{write::Writer, Encode, Encoder, EncoderImpl},
|
||||
error::{DecodeError, EncodeError},
|
||||
|
|
@ -13,17 +13,13 @@ use std::{
|
|||
time::SystemTime,
|
||||
};
|
||||
|
||||
/// Decode type `D` from the given reader. The reader can be any type that implements `std::io::Read`, e.g. `std::fs::File`.
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
|
||||
pub fn decode_from<D: Decode, R: std::io::Read>(src: &mut R) -> Result<D, DecodeError> {
|
||||
decode_from_with_config(src, config::Configuration::standard())
|
||||
}
|
||||
|
||||
/// Decode type `D` from the given reader with the given `Config`. The reader can be any type that implements `std::io::Read`, e.g. `std::fs::File`.
|
||||
///
|
||||
/// See the [config] module for more information about config options.
|
||||
///
|
||||
/// [config]: config/index.html
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
|
||||
pub fn decode_from_with_config<D: Decode, C: Config, R: std::io::Read>(
|
||||
pub fn decode_from_reader<D: Decode, C: Config, R: std::io::Read>(
|
||||
src: &mut R,
|
||||
_config: C,
|
||||
) -> Result<D, DecodeError> {
|
||||
|
|
@ -41,18 +37,12 @@ impl<R: std::io::Read> Reader for R {
|
|||
}
|
||||
}
|
||||
|
||||
/// Encode the given value into any type that implements `std::io::Write`, e.g. `std::fs::File`.
|
||||
/// Encode the given value into any type that implements `std::io::Write`, e.g. `std::fs::File`, with the given `Config`.
|
||||
/// See the [config] module for more information.
|
||||
///
|
||||
/// [config]: config/index.html
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
|
||||
pub fn encode_into_write<E: Encode, W: std::io::Write>(
|
||||
val: E,
|
||||
dst: &mut W,
|
||||
) -> Result<usize, EncodeError> {
|
||||
encode_into_write_with_config(val, dst, config::Configuration::standard())
|
||||
}
|
||||
|
||||
/// Encode the given value into any type that implements `std::io::Write`, e.g. `std::fs::File`, with the given `Config`. See the [config] module for more information.
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
|
||||
pub fn encode_into_write_with_config<E: Encode, C: Config, W: std::io::Write>(
|
||||
pub fn encode_into_writer<E: Encode, C: Config, W: std::io::Write>(
|
||||
val: E,
|
||||
dst: &mut W,
|
||||
config: C,
|
||||
|
|
|
|||
39
src/lib.rs
39
src/lib.rs
|
|
@ -15,11 +15,11 @@
|
|||
//!
|
||||
//! |Name |Default?|Supported types for Encode/Decode|Enabled methods |Other|
|
||||
//! |------|--------|-----------------------------------------|-----------------------------------------------------------------|-----|
|
||||
//! |std | Yes ||`decode_from[_with_config]` and `encode_into_write[_with_config]`|
|
||||
//! |alloc | Yes |All common containers in alloc, like `Vec`, `String`, `Box`|`encode_to_vec[_with_config]`|
|
||||
//! |std | Yes ||`decode_from_reader` and `encode_into_writer`|
|
||||
//! |alloc | Yes |All common containers in alloc, like `Vec`, `String`, `Box`|`encode_to_vec`|
|
||||
//! |atomic| Yes |All `Atomic*` integer types, e.g. `AtomicUsize`, and `AtomicBool`||
|
||||
//! |derive| Yes |||Enables the `Encode` and `Decode` derive macro|
|
||||
//! |serde | No ||`serde_decode_from[_with_config]`, `serde_encode_into[_with_config]`|Also enables `_to_vec` when `alloc` is enabled|
|
||||
//! |serde | No |TODO|TODO|TODO|
|
||||
|
||||
#![doc(html_root_url = "https://docs.rs/bincode/2.0.0-alpha.0")]
|
||||
#![crate_name = "bincode"]
|
||||
|
|
@ -43,22 +43,12 @@ pub mod error;
|
|||
|
||||
use config::Config;
|
||||
|
||||
/// Encode the given value into the given slice. Returns the amount of bytes that have been written.
|
||||
///
|
||||
/// Will take the [standard] configuration. See the [config] module for more information.
|
||||
///
|
||||
/// [standard]: config/struct.Configuration.html#method.standard
|
||||
pub fn encode_into_slice<E: enc::Encode>(
|
||||
val: E,
|
||||
dst: &mut [u8],
|
||||
) -> Result<usize, error::EncodeError> {
|
||||
encode_into_slice_with_config(val, dst, config::Configuration::standard())
|
||||
}
|
||||
|
||||
/// Encode the given value into the given slice. Returns the amount of bytes that have been written.
|
||||
///
|
||||
/// See the [config] module for more information on configurations.
|
||||
pub fn encode_into_slice_with_config<E: enc::Encode, C: Config>(
|
||||
///
|
||||
/// [config]: config/index.html
|
||||
pub fn encode_into_slice<E: enc::Encode, C: Config>(
|
||||
val: E,
|
||||
dst: &mut [u8],
|
||||
config: C,
|
||||
|
|
@ -69,22 +59,13 @@ pub fn encode_into_slice_with_config<E: enc::Encode, C: Config>(
|
|||
Ok(encoder.into_writer().bytes_written())
|
||||
}
|
||||
|
||||
/// Attempt to decode a given type `D` from the given slice.
|
||||
///
|
||||
/// Will take the [Default] configuration. See the [config] module for more information.
|
||||
///
|
||||
/// [Default]: config/struct.Default.html
|
||||
pub fn decode<'__de, D: de::BorrowDecode<'__de>>(
|
||||
src: &'__de [u8],
|
||||
) -> Result<D, error::DecodeError> {
|
||||
decode_with_config(src, config::Configuration::standard())
|
||||
}
|
||||
|
||||
/// Attempt to decode a given type `D` from the given slice.
|
||||
///
|
||||
/// See the [config] module for more information on configurations.
|
||||
pub fn decode_with_config<'__de, D: de::BorrowDecode<'__de>, C: Config>(
|
||||
src: &'__de [u8],
|
||||
///
|
||||
/// [config]: config/index.html
|
||||
pub fn decode_from_slice<'a, D: de::BorrowDecode<'a>, C: Config>(
|
||||
src: &'a [u8],
|
||||
_config: C,
|
||||
) -> Result<D, error::DecodeError> {
|
||||
let reader = de::read::SliceReader::new(src);
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ use alloc::collections::*;
|
|||
use alloc::rc::Rc;
|
||||
#[cfg(feature = "atomic")]
|
||||
use alloc::sync::Arc;
|
||||
use bincode::config::Configuration;
|
||||
use utils::{the_same, the_same_with_comparer};
|
||||
|
||||
struct Foo {
|
||||
|
|
@ -40,10 +41,10 @@ impl bincode::de::Decode for Foo {
|
|||
|
||||
#[test]
|
||||
fn test_vec() {
|
||||
let vec = bincode::encode_to_vec(Foo { a: 5, b: 10 }).unwrap();
|
||||
let vec = bincode::encode_to_vec(Foo { a: 5, b: 10 }, Configuration::standard()).unwrap();
|
||||
assert_eq!(vec, &[5, 10]);
|
||||
|
||||
let foo: Foo = bincode::decode(&vec).unwrap();
|
||||
let foo: Foo = bincode::decode_from_slice(&vec, Configuration::standard()).unwrap();
|
||||
assert_eq!(foo.a, 5);
|
||||
assert_eq!(foo.b, 10);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
mod utils;
|
||||
|
||||
use bincode::config::Configuration;
|
||||
use core::cell::{Cell, RefCell};
|
||||
use core::ops::Bound;
|
||||
use core::time::Duration;
|
||||
|
|
@ -119,7 +120,7 @@ fn test_refcell_already_borrowed() {
|
|||
let _mutable_guard = cell.borrow_mut();
|
||||
// now try to encode it
|
||||
let mut slice = [0u8; 10];
|
||||
let result = bincode::encode_into_slice(&cell, &mut slice)
|
||||
let result = bincode::encode_into_slice(&cell, &mut slice, Configuration::standard())
|
||||
.expect_err("Encoding a borrowed refcell should fail");
|
||||
|
||||
match result {
|
||||
|
|
@ -132,10 +133,11 @@ fn test_refcell_already_borrowed() {
|
|||
fn test_slice() {
|
||||
let mut buffer = [0u8; 32];
|
||||
let input: &[u8] = &[1, 2, 3, 4, 5, 6, 7];
|
||||
bincode::encode_into_slice(input, &mut buffer).unwrap();
|
||||
bincode::encode_into_slice(input, &mut buffer, Configuration::standard()).unwrap();
|
||||
assert_eq!(&buffer[..8], &[7, 1, 2, 3, 4, 5, 6, 7]);
|
||||
|
||||
let output: &[u8] = bincode::decode(&mut buffer[..8]).unwrap();
|
||||
let output: &[u8] =
|
||||
bincode::decode_from_slice(&mut buffer[..8], Configuration::standard()).unwrap();
|
||||
assert_eq!(input, output);
|
||||
}
|
||||
|
||||
|
|
@ -143,13 +145,14 @@ fn test_slice() {
|
|||
fn test_str() {
|
||||
let mut buffer = [0u8; 32];
|
||||
let input: &str = "Hello world";
|
||||
bincode::encode_into_slice(input, &mut buffer).unwrap();
|
||||
bincode::encode_into_slice(input, &mut buffer, Configuration::standard()).unwrap();
|
||||
assert_eq!(
|
||||
&buffer[..12],
|
||||
&[11, 72, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100]
|
||||
);
|
||||
|
||||
let output: &str = bincode::decode(&mut buffer[..12]).unwrap();
|
||||
let output: &str =
|
||||
bincode::decode_from_slice(&mut buffer[..12], Configuration::standard()).unwrap();
|
||||
assert_eq!(input, output);
|
||||
}
|
||||
|
||||
|
|
@ -157,9 +160,10 @@ fn test_str() {
|
|||
fn test_array() {
|
||||
let mut buffer = [0u8; 32];
|
||||
let input: [u8; 10] = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100];
|
||||
bincode::encode_into_slice(input, &mut buffer).unwrap();
|
||||
bincode::encode_into_slice(input, &mut buffer, Configuration::standard()).unwrap();
|
||||
assert_eq!(&buffer[..10], &[10, 20, 30, 40, 50, 60, 70, 80, 90, 100]);
|
||||
|
||||
let output: [u8; 10] = bincode::decode(&mut buffer[..10]).unwrap();
|
||||
let output: [u8; 10] =
|
||||
bincode::decode_from_slice(&mut buffer[..10], Configuration::standard()).unwrap();
|
||||
assert_eq!(input, output);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
#![cfg(feature = "derive")]
|
||||
|
||||
use bincode::config::Configuration;
|
||||
use bincode::{de::Decode, enc::Encode};
|
||||
|
||||
#[derive(bincode::Encode, PartialEq, Debug)]
|
||||
|
|
@ -48,7 +49,8 @@ fn test_encode() {
|
|||
c: 20u8,
|
||||
};
|
||||
let mut slice = [0u8; 1024];
|
||||
let bytes_written = bincode::encode_into_slice(start, &mut slice).unwrap();
|
||||
let bytes_written =
|
||||
bincode::encode_into_slice(start, &mut slice, Configuration::standard()).unwrap();
|
||||
assert_eq!(bytes_written, 3);
|
||||
assert_eq!(&slice[..bytes_written], &[10, 10, 20]);
|
||||
}
|
||||
|
|
@ -62,7 +64,8 @@ fn test_decode() {
|
|||
c: 1024u32,
|
||||
};
|
||||
let slice = [5, 10, 251, 0, 4];
|
||||
let result: Test2<u32> = bincode::decode_from(&mut slice.as_ref()).unwrap();
|
||||
let result: Test2<u32> =
|
||||
bincode::decode_from_reader(&mut slice.as_ref(), Configuration::standard()).unwrap();
|
||||
assert_eq!(result, start);
|
||||
}
|
||||
|
||||
|
|
@ -70,7 +73,8 @@ fn test_decode() {
|
|||
fn test_encode_tuple() {
|
||||
let start = TestTupleStruct(5, 10, 1024);
|
||||
let mut slice = [0u8; 1024];
|
||||
let bytes_written = bincode::encode_into_slice(start, &mut slice).unwrap();
|
||||
let bytes_written =
|
||||
bincode::encode_into_slice(start, &mut slice, Configuration::standard()).unwrap();
|
||||
assert_eq!(bytes_written, 5);
|
||||
assert_eq!(&slice[..bytes_written], &[5, 10, 251, 0, 4]);
|
||||
}
|
||||
|
|
@ -79,7 +83,8 @@ fn test_encode_tuple() {
|
|||
fn test_decode_tuple() {
|
||||
let start = TestTupleStruct(5, 10, 1024);
|
||||
let mut slice = [5, 10, 251, 0, 4];
|
||||
let result: TestTupleStruct = bincode::decode(&mut slice).unwrap();
|
||||
let result: TestTupleStruct =
|
||||
bincode::decode_from_slice(&mut slice, Configuration::standard()).unwrap();
|
||||
assert_eq!(result, start);
|
||||
}
|
||||
|
||||
|
|
@ -87,7 +92,8 @@ fn test_decode_tuple() {
|
|||
fn test_encode_enum_struct_variant() {
|
||||
let start = TestEnum::Bar { name: 5u32 };
|
||||
let mut slice = [0u8; 1024];
|
||||
let bytes_written = bincode::encode_into_slice(start, &mut slice).unwrap();
|
||||
let bytes_written =
|
||||
bincode::encode_into_slice(start, &mut slice, Configuration::standard()).unwrap();
|
||||
assert_eq!(bytes_written, 2);
|
||||
assert_eq!(&slice[..bytes_written], &[1, 5]);
|
||||
}
|
||||
|
|
@ -96,7 +102,8 @@ fn test_encode_enum_struct_variant() {
|
|||
fn test_decode_enum_struct_variant() {
|
||||
let start = TestEnum::Bar { name: 5u32 };
|
||||
let mut slice = [1, 5];
|
||||
let result: TestEnum = bincode::decode(&mut slice).unwrap();
|
||||
let result: TestEnum =
|
||||
bincode::decode_from_slice(&mut slice, Configuration::standard()).unwrap();
|
||||
assert_eq!(result, start);
|
||||
}
|
||||
|
||||
|
|
@ -104,7 +111,8 @@ fn test_decode_enum_struct_variant() {
|
|||
fn test_encode_enum_tuple_variant() {
|
||||
let start = TestEnum::Baz(5, 10, 1024);
|
||||
let mut slice = [0u8; 1024];
|
||||
let bytes_written = bincode::encode_into_slice(start, &mut slice).unwrap();
|
||||
let bytes_written =
|
||||
bincode::encode_into_slice(start, &mut slice, Configuration::standard()).unwrap();
|
||||
assert_eq!(bytes_written, 6);
|
||||
assert_eq!(&slice[..bytes_written], &[2, 5, 10, 251, 0, 4]);
|
||||
}
|
||||
|
|
@ -113,7 +121,8 @@ fn test_encode_enum_tuple_variant() {
|
|||
fn test_decode_enum_unit_variant() {
|
||||
let start = TestEnum::Foo;
|
||||
let mut slice = [0];
|
||||
let result: TestEnum = bincode::decode(&mut slice).unwrap();
|
||||
let result: TestEnum =
|
||||
bincode::decode_from_slice(&mut slice, Configuration::standard()).unwrap();
|
||||
assert_eq!(result, start);
|
||||
}
|
||||
|
||||
|
|
@ -121,7 +130,8 @@ fn test_decode_enum_unit_variant() {
|
|||
fn test_encode_enum_unit_variant() {
|
||||
let start = TestEnum::Foo;
|
||||
let mut slice = [0u8; 1024];
|
||||
let bytes_written = bincode::encode_into_slice(start, &mut slice).unwrap();
|
||||
let bytes_written =
|
||||
bincode::encode_into_slice(start, &mut slice, Configuration::standard()).unwrap();
|
||||
assert_eq!(bytes_written, 1);
|
||||
assert_eq!(&slice[..bytes_written], &[0]);
|
||||
}
|
||||
|
|
@ -130,6 +140,7 @@ fn test_encode_enum_unit_variant() {
|
|||
fn test_decode_enum_tuple_variant() {
|
||||
let start = TestEnum::Baz(5, 10, 1024);
|
||||
let mut slice = [2, 5, 10, 251, 0, 4];
|
||||
let result: TestEnum = bincode::decode(&mut slice).unwrap();
|
||||
let result: TestEnum =
|
||||
bincode::decode_from_slice(&mut slice, Configuration::standard()).unwrap();
|
||||
assert_eq!(result, start);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
#![cfg(all(feature = "serde", feature = "alloc", feature = "derive"))]
|
||||
|
||||
use bincode::config::Configuration;
|
||||
use serde_derive::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Serialize, Deserialize, bincode::Encode, bincode::Decode)]
|
||||
|
|
@ -20,9 +21,11 @@ fn test_serde_round_trip() {
|
|||
assert_eq!(result.b, 0);
|
||||
|
||||
// validate bincode working
|
||||
let bytes = bincode::encode_to_vec(SerdeRoundtrip { a: 15, b: 15 }).unwrap();
|
||||
let bytes =
|
||||
bincode::encode_to_vec(SerdeRoundtrip { a: 15, b: 15 }, Configuration::standard()).unwrap();
|
||||
assert_eq!(bytes, &[15, 15]);
|
||||
let result: SerdeRoundtrip = bincode::decode(&bytes).unwrap();
|
||||
let result: SerdeRoundtrip =
|
||||
bincode::decode_from_slice(&bytes, Configuration::standard()).unwrap();
|
||||
assert_eq!(result.a, 15);
|
||||
assert_eq!(result.b, 15);
|
||||
}
|
||||
|
|
|
|||
17
tests/std.rs
17
tests/std.rs
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
mod utils;
|
||||
|
||||
use bincode::config::Configuration;
|
||||
use std::{
|
||||
ffi::{CStr, CString},
|
||||
io::{Cursor, Seek, SeekFrom},
|
||||
|
|
@ -43,7 +44,7 @@ impl bincode::de::Decode for Foo {
|
|||
#[test]
|
||||
fn test_std_cursor() {
|
||||
let mut cursor = Cursor::<&[u8]>::new(&[5, 10]);
|
||||
let foo: Foo = bincode::decode_from(&mut cursor).unwrap();
|
||||
let foo: Foo = bincode::decode_from_reader(&mut cursor, Configuration::standard()).unwrap();
|
||||
|
||||
assert_eq!(foo.a, 5);
|
||||
assert_eq!(foo.b, 10);
|
||||
|
|
@ -53,11 +54,13 @@ fn test_std_cursor() {
|
|||
fn test_std_file() {
|
||||
let mut file = tempfile::tempfile().expect("Could not create temp file");
|
||||
|
||||
let bytes_written = bincode::encode_into_write(Foo { a: 30, b: 50 }, &mut file).unwrap();
|
||||
let bytes_written =
|
||||
bincode::encode_into_writer(Foo { a: 30, b: 50 }, &mut file, Configuration::standard())
|
||||
.unwrap();
|
||||
assert_eq!(bytes_written, 2);
|
||||
file.seek(SeekFrom::Start(0)).unwrap();
|
||||
|
||||
let foo: Foo = bincode::decode_from(&mut file).unwrap();
|
||||
let foo: Foo = bincode::decode_from_reader(&mut file, Configuration::standard()).unwrap();
|
||||
|
||||
assert_eq!(foo.a, 30);
|
||||
assert_eq!(foo.b, 50);
|
||||
|
|
@ -96,13 +99,13 @@ fn test_std_commons() {
|
|||
|
||||
// &CStr
|
||||
let cstr = CStr::from_bytes_with_nul(b"Hello world\0").unwrap();
|
||||
let len = bincode::encode_into_slice_with_config(cstr, &mut buffer, config).unwrap();
|
||||
let decoded: &CStr = bincode::decode_with_config(&mut buffer[..len], config).unwrap();
|
||||
let len = bincode::encode_into_slice(cstr, &mut buffer, config).unwrap();
|
||||
let decoded: &CStr = bincode::decode_from_slice(&mut buffer[..len], config).unwrap();
|
||||
assert_eq!(cstr, decoded);
|
||||
|
||||
// Path
|
||||
let path = Path::new("C:/Program Files/Foo");
|
||||
let len = bincode::encode_into_slice_with_config(path, &mut buffer, config).unwrap();
|
||||
let decoded: &Path = bincode::decode_with_config(&mut buffer[..len], config).unwrap();
|
||||
let len = bincode::encode_into_slice(path, &mut buffer, config).unwrap();
|
||||
let decoded: &Path = bincode::decode_from_slice(&mut buffer[..len], config).unwrap();
|
||||
assert_eq!(path, decoded);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,14 +8,14 @@ where
|
|||
CMP: Fn(&V, &V) -> bool,
|
||||
{
|
||||
let mut buffer = [0u8; 2048];
|
||||
let len = bincode::encode_into_slice_with_config(&element, &mut buffer, config).unwrap();
|
||||
let len = bincode::encode_into_slice(&element, &mut buffer, config).unwrap();
|
||||
println!(
|
||||
"{:?}: {:?} ({:?})",
|
||||
element,
|
||||
&buffer[..len],
|
||||
core::any::type_name::<C>()
|
||||
);
|
||||
let decoded: V = bincode::decode_with_config(&mut buffer, config).unwrap();
|
||||
let decoded: V = bincode::decode_from_slice(&mut buffer, config).unwrap();
|
||||
|
||||
assert!(
|
||||
cmp(&element, &decoded),
|
||||
|
|
|
|||
Loading…
Reference in New Issue