Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed clippy warning and updated DecodeError #574

Merged
merged 1 commit into from
Aug 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compatibility/src/membership.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::collections::BTreeSet;
type NodeId = u64;

#[derive(
bincode_2::Encode, bincode_2::Decode, serde::Serialize, serde::Deserialize, Debug, PartialEq,
bincode_2::Encode, bincode_2::Decode, serde::Serialize, serde::Deserialize, Debug, PartialEq, Eq,
)]
#[bincode(crate = "bincode_2")]
pub struct Membership {
Expand Down
2 changes: 1 addition & 1 deletion compatibility/src/rand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use rand::Rng;

#[derive(
Debug, bincode_2::Encode, bincode_2::Decode, serde::Serialize, serde::Deserialize, PartialEq,
Debug, bincode_2::Encode, bincode_2::Decode, serde::Serialize, serde::Deserialize, PartialEq, Eq,
)]
#[bincode(crate = "bincode_2")]
pub struct Lcg64Xsh32 {
Expand Down
8 changes: 4 additions & 4 deletions compatibility/src/sway.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,30 @@ fn random(rng: &mut impl Rng) -> FTXresponse<Trade> {
}
}

#[derive(bincode_2::Encode, bincode_2::Decode, Serialize, Deserialize, Debug, PartialEq)]
#[derive(bincode_2::Encode, bincode_2::Decode, Serialize, Deserialize, Debug, PartialEq, Eq)]
#[bincode(crate = "bincode_2")]
pub enum FTXresponse<T> {
Result(FTXresponseSuccess<T>),
Error(FTXresponseFailure),
}

#[derive(
bincode_2::Encode, bincode_2::Decode, serde::Serialize, serde::Deserialize, Debug, PartialEq,
bincode_2::Encode, bincode_2::Decode, serde::Serialize, serde::Deserialize, Debug, PartialEq, Eq,
)]
#[bincode(crate = "bincode_2")]
pub struct FTXresponseSuccess<T> {
pub success: bool,
pub result: T,
}

#[derive(bincode_2::Encode, bincode_2::Decode, Serialize, Deserialize, Debug, PartialEq)]
#[derive(bincode_2::Encode, bincode_2::Decode, Serialize, Deserialize, Debug, PartialEq, Eq)]
#[bincode(crate = "bincode_2")]
pub struct FTXresponseFailure {
pub success: bool,
pub error: String,
}

#[derive(bincode_2::Encode, bincode_2::Decode, Serialize, Deserialize, Debug, PartialEq)]
#[derive(bincode_2::Encode, bincode_2::Decode, Serialize, Deserialize, Debug, PartialEq, Eq)]
#[bincode(crate = "bincode_2")]
pub enum TradeSide {
Buy,
Expand Down
18 changes: 16 additions & 2 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ impl core::fmt::Display for EncodeError {

/// Errors that can be encountered by decoding a type
#[non_exhaustive]
#[derive(Debug, PartialEq)]
#[derive(Debug)]
pub enum DecodeError {
/// The reader reached its end but more bytes were expected.
UnexpectedEnd {
Expand Down Expand Up @@ -164,6 +164,20 @@ pub enum DecodeError {
position: usize,
},

/// The reader encountered an IO error but more bytes were expected.
#[cfg(feature = "std")]
Io {
/// The IO error expected
inner: std::io::Error,

/// Gives an estimate of how many extra bytes are needed.
///
/// **Note**: this is only an estimate and not indicative of the actual bytes needed.
///
/// **Note**: Bincode has no look-ahead mechanism. This means that this will only return the amount of bytes to be read for the current action, and not take into account the entire data structure being read.
additional: usize,
},

/// An uncommon error occurred, see the inner text for more information
#[cfg(feature = "alloc")]
OtherString(alloc::string::String),
Expand Down Expand Up @@ -199,7 +213,7 @@ impl DecodeError {

/// Indicates which enum variants are allowed
#[non_exhaustive]
#[derive(Debug, PartialEq)]
#[derive(Debug, PartialEq, Eq)]
pub enum AllowedEnumVariants {
/// All values between `min` and `max` (inclusive) are allowed
#[allow(missing_docs)]
Expand Down
20 changes: 9 additions & 11 deletions src/features/impl_std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ where
{
#[inline(always)]
fn read(&mut self, bytes: &mut [u8]) -> Result<(), DecodeError> {
match self.reader.read_exact(bytes) {
Ok(_) => Ok(()),
Err(_) => Err(DecodeError::UnexpectedEnd {
self.reader
.read_exact(bytes)
.map_err(|inner| DecodeError::Io {
inner,
additional: bytes.len(),
}),
}
})
}
}

Expand All @@ -62,12 +62,10 @@ where
R: std::io::Read,
{
fn read(&mut self, bytes: &mut [u8]) -> Result<(), DecodeError> {
match self.read_exact(bytes) {
Ok(_) => Ok(()),
Err(_) => Err(DecodeError::UnexpectedEnd {
additional: bytes.len(),
}),
}
self.read_exact(bytes).map_err(|inner| DecodeError::Io {
inner,
additional: bytes.len(),
})
}

#[inline]
Expand Down
4 changes: 2 additions & 2 deletions src/features/serde/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ pub use self::de_owned::*;
pub use self::ser::*;

/// A serde-specific error that occurred while decoding.
#[derive(Debug, PartialEq)]
#[derive(Debug)]
#[non_exhaustive]
pub enum DecodeError {
/// Bincode does not support serde's `any` decoding feature
Expand Down Expand Up @@ -124,7 +124,7 @@ impl Into<crate::error::DecodeError> for DecodeError {
}

/// A serde-specific error that occurred while encoding.
#[derive(Debug, PartialEq)]
#[derive(Debug)]
#[non_exhaustive]
pub enum EncodeError {
/// Serde provided bincode with a sequence without a length, which is not supported in bincode
Expand Down
14 changes: 8 additions & 6 deletions tests/basic_types.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod utils;

use bincode::error::DecodeError;
use core::cell::{Cell, RefCell};
use core::ops::Bound;
use core::time::Duration;
Expand Down Expand Up @@ -253,13 +254,14 @@ fn test_duration_out_of_range() {
let result: Result<(std::time::Duration, usize), _> =
bincode::decode_from_slice(&input, bincode::config::standard());

assert_eq!(
result.unwrap_err(),
bincode::error::DecodeError::InvalidDuration {
match result {
Err(DecodeError::InvalidDuration {
secs: u64::MAX,
nanos: u32::MAX
}
);
nanos: u32::MAX,
}) => {}
Err(e) => panic!("Expected InvalidDuration, got {:?}", e),
Ok(_) => panic!("Expected the decode to fail"),
}
}

#[test]
Expand Down
63 changes: 37 additions & 26 deletions tests/derive.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#![cfg(feature = "derive")]

use bincode::error::DecodeError;

#[derive(bincode::Encode, PartialEq, Debug)]
pub(crate) struct Test<T> {
a: T,
Expand Down Expand Up @@ -276,28 +278,38 @@ fn test_c_style_enum() {
assert_eq!(ser(CStyleEnum::D), 5);
assert_eq!(ser(CStyleEnum::E), 6);

fn de(num: u8) -> Result<CStyleEnum, bincode::error::DecodeError> {
let (result, len) = bincode::decode_from_slice(&[num], bincode::config::standard())?;
assert_eq!(len, 1);
Ok(result)
fn assert_de_successfully(num: u8, expected: CStyleEnum) {
match bincode::decode_from_slice::<CStyleEnum, _>(&[num], bincode::config::standard()) {
Ok((result, len)) => {
assert_eq!(len, 1);
assert_eq!(result, expected)
}
Err(e) => panic!("Could not deserialize CStyleEnum idx {num}: {e:?}"),
}
}

fn expected_err(idx: u32) -> Result<CStyleEnum, bincode::error::DecodeError> {
Err(bincode::error::DecodeError::UnexpectedVariant {
type_name: "CStyleEnum",
allowed: &bincode::error::AllowedEnumVariants::Allowed(&[1, 2, 3, 5, 6]),
found: idx,
})
fn assert_de_fails(num: u8) {
match bincode::decode_from_slice::<CStyleEnum, _>(&[num], bincode::config::standard()) {
Ok(_) => {
panic!("Expected to not be able to decode CStyleEnum index {num}, but it succeeded")
}
Err(DecodeError::UnexpectedVariant {
type_name: "CStyleEnum",
allowed: &bincode::error::AllowedEnumVariants::Allowed(&[1, 2, 3, 5, 6]),
found,
}) if found == num as u32 => {}
Err(e) => panic!("Expected DecodeError::UnexpectedVariant, got {e:?}"),
}
}

assert_eq!(de(0), expected_err(0));
assert_eq!(de(1).unwrap(), CStyleEnum::A);
assert_eq!(de(2).unwrap(), CStyleEnum::B);
assert_eq!(de(3).unwrap(), CStyleEnum::C);
assert_eq!(de(4), expected_err(4));
assert_eq!(de(5).unwrap(), CStyleEnum::D);
assert_eq!(de(6).unwrap(), CStyleEnum::E);
assert_eq!(de(7), expected_err(7));
assert_de_fails(0);
assert_de_successfully(1, CStyleEnum::A);
assert_de_successfully(2, CStyleEnum::B);
assert_de_successfully(3, CStyleEnum::C);
assert_de_fails(4);
assert_de_successfully(5, CStyleEnum::D);
assert_de_successfully(6, CStyleEnum::E);
assert_de_fails(7);
}

macro_rules! macro_newtype {
Expand Down Expand Up @@ -344,14 +356,13 @@ pub enum BorrowedEmptyEnum {}

#[test]
fn test_empty_enum_decode() {
let err =
bincode::decode_from_slice::<EmptyEnum, _>(&[], bincode::config::standard()).unwrap_err();
assert_eq!(
err,
bincode::error::DecodeError::EmptyEnum {
type_name: "derive::EmptyEnum"
}
);
match bincode::decode_from_slice::<EmptyEnum, _>(&[], bincode::config::standard()) {
Ok(_) => panic!("We successfully decoded an empty slice, this should never happen"),
Err(DecodeError::EmptyEnum {
type_name: "derive::EmptyEnum",
}) => {}
Err(e) => panic!("Expected DecodeError::EmptyEnum, got {e:?}"),
}
}

#[derive(bincode::Encode, bincode::Decode, PartialEq, Debug, Eq)]
Expand Down
4 changes: 2 additions & 2 deletions tests/issues/issue_427.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/// # Remarks
/// Used to store HID-IO data chunks. Will be chunked into individual packets on transmission.
#[repr(C)]
#[derive(PartialEq, Clone, Debug, bincode::Encode)]
#[derive(PartialEq, Eq, Clone, Debug, bincode::Encode)]
pub struct HidIoPacketBuffer<const H: usize> {
/// Type of packet (Continued is automatically set if needed)
pub ptype: u32,
Expand All @@ -20,7 +20,7 @@ pub struct HidIoPacketBuffer<const H: usize> {
}

#[repr(u32)]
#[derive(PartialEq, Clone, Copy, Debug, bincode::Encode)]
#[derive(PartialEq, Eq, Clone, Copy, Debug, bincode::Encode)]
#[allow(dead_code)]
/// Requests for to perform a specific action
pub enum HidIoCommandId {
Expand Down
4 changes: 2 additions & 2 deletions tests/issues/issue_474.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ use std::collections::HashMap;
use std::prelude::rust_2021::*;
use uuid::Uuid;

#[derive(serde_derive::Serialize, serde_derive::Deserialize, PartialEq, Debug)]
#[derive(serde_derive::Serialize, serde_derive::Deserialize, PartialEq, Eq, Debug)]
pub struct MyStruct {
name: String,
}

#[derive(serde_derive::Serialize, serde_derive::Deserialize, PartialEq, Debug)]
#[derive(serde_derive::Serialize, serde_derive::Deserialize, PartialEq, Eq, Debug)]
pub struct CustomerTest {
pub id: Option<Uuid>,
pub email_address: Option<String>,
Expand Down
1 change: 1 addition & 0 deletions tests/issues/issue_500.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use std::collections::BTreeSet;
serde_derive::Deserialize,
Debug,
PartialEq,
Eq,
)]
pub struct Membership {
/// learners set
Expand Down
10 changes: 5 additions & 5 deletions tests/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ fn test_serde_round_trip() {
assert_eq!(len, 13);
}

#[derive(Serialize, Deserialize, PartialEq, Debug)]
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug)]
pub struct SerdeWithBorrowedData<'a> {
pub a: u32,
#[serde(skip)]
Expand Down Expand Up @@ -95,7 +95,7 @@ fn test_serialize_deserialize_borrowed_data() {
);
}

#[derive(Serialize, Deserialize, PartialEq, Debug)]
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug)]
pub struct SerdeWithOwnedData {
pub a: u32,
#[serde(skip)]
Expand Down Expand Up @@ -146,18 +146,18 @@ mod derive {
use bincode::{Decode, Encode};
use serde_derive::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, PartialEq, Debug)]
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug)]
pub struct SerdeType {
pub a: u32,
}

#[derive(Decode, Encode, PartialEq, Debug)]
#[derive(Decode, Encode, PartialEq, Eq, Debug)]
pub struct StructWithSerde {
#[bincode(with_serde)]
pub serde: SerdeType,
}

#[derive(Decode, Encode, PartialEq, Debug)]
#[derive(Decode, Encode, PartialEq, Eq, Debug)]
pub enum EnumWithSerde {
Unit(#[bincode(with_serde)] SerdeType),
Struct {
Expand Down
15 changes: 10 additions & 5 deletions tests/std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

mod utils;

use bincode::error::DecodeError;
use std::{
ffi::{CStr, CString},
io::{Cursor, Seek, SeekFrom},
Expand Down Expand Up @@ -149,12 +150,16 @@ fn test_system_time_out_of_range() {
let result: Result<(std::time::SystemTime, usize), _> =
bincode::decode_from_slice(&input, bincode::config::standard());

assert_eq!(
result.unwrap_err(),
bincode::error::DecodeError::InvalidSystemTime {
duration: std::time::Duration::new(10447520527445462160, 144),
match result {
Ok(_) => panic!("Expected the decode to fail, but it succeeded"),
Err(DecodeError::InvalidSystemTime { duration }) => {
assert_eq!(
duration,
std::time::Duration::new(10447520527445462160, 144)
)
}
);
Err(e) => panic!("Expected DecodeError::InvalidSystemTime, got {e:?}"),
}
}

/// Simple example of user-defined hasher to test encoding/decoding HashMap and HashSet with custom hash algorithms.
Expand Down
2 changes: 1 addition & 1 deletion tests/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ where
let mut buffer = [0u8; 2048];
let len = bincode::serde::encode_into_slice(&element, &mut buffer, config);

let decoded = bincode::serde::decode_from_slice(&mut buffer, config);
let decoded = bincode::serde::decode_from_slice(&buffer, config);

if !C::SKIP_FIXED_ARRAY_LENGTH {
let len = len.unwrap();
Expand Down