Fix riscv32 atomics and fix tests on 32-bit platforms (#533)

* tests: fix alloc range test on 32-bit Windows

This test checks the range, which is necessarily different when running
on another platform.

Signed-off-by: Sean Cross <sean@xobs.io>

* enc: case usize/isize as u64/i64 in serialization

The deserialization process assumes that usize/isize are 64-bits, as
does the spec at
https://github.com/bincode-org/bincode/blob/trunk/docs/spec.md#varintencoding

Force `usize` and `isize` to be encoded as `u64` and `i64` to force
32-bit platforms to conform to this spec.

This fixes running tests under `cargo test --target i686-pc-windows-msvc`

Signed-off-by: Sean Cross <sean@xobs.io>

* atomic: only provide Atomic*64 on supported platforms

Not all platforms support AtomicI64 or AtomicU64. Use the
`target_has_atomic` config flag to determine whether to include these.

This fixes #532.

Signed-off-by: Sean Cross <sean@xobs.io>

* atomic: remove feature and use new attributes

Now that there is an attribute to indicate which atomic features are
supported on this platform, remove the `atomic` Feature and use these
new attributes.

Signed-off-by: Sean Cross <sean@xobs.io>

* alloc: run `cargo fmt`

Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
Sean Cross 2022-04-08 17:35:13 +08:00 committed by GitHub
parent 883278fa0c
commit d7edfaa0b7
5 changed files with 44 additions and 11 deletions

View File

@ -24,10 +24,9 @@ description = "A binary serialization / deserialization strategy for transformin
edition = "2021"
[features]
default = ["std", "derive", "atomic"]
default = ["std", "derive"]
std = ["alloc"]
alloc = []
atomic = []
derive = ["bincode_derive"]
# BlockedTODO: https://github.com/rust-lang/cargo/issues/8832

View File

@ -1,9 +1,22 @@
use crate::{de::Decode, enc::Encode};
use core::sync::atomic::{
AtomicBool, AtomicI16, AtomicI32, AtomicI64, AtomicI8, AtomicIsize, AtomicU16, AtomicU32,
AtomicU64, AtomicU8, AtomicUsize, Ordering,
};
use core::sync::atomic::Ordering;
#[cfg(target_has_atomic = "ptr")]
use core::sync::atomic::{AtomicIsize, AtomicUsize};
#[cfg(target_has_atomic = "8")]
use core::sync::atomic::{AtomicBool, AtomicI8, AtomicU8};
#[cfg(target_has_atomic = "16")]
use core::sync::atomic::{AtomicI16, AtomicU16};
#[cfg(target_has_atomic = "32")]
use core::sync::atomic::{AtomicI32, AtomicU32};
#[cfg(target_has_atomic = "64")]
use core::sync::atomic::{AtomicI64, AtomicU64};
#[cfg(target_has_atomic = "8")]
impl Encode for AtomicBool {
fn encode<E: crate::enc::Encoder>(
&self,
@ -13,12 +26,14 @@ impl Encode for AtomicBool {
}
}
#[cfg(target_has_atomic = "8")]
impl Decode for AtomicBool {
fn decode<D: crate::de::Decoder>(decoder: &mut D) -> Result<Self, crate::error::DecodeError> {
Ok(AtomicBool::new(Decode::decode(decoder)?))
}
}
#[cfg(target_has_atomic = "8")]
impl Encode for AtomicU8 {
fn encode<E: crate::enc::Encoder>(
&self,
@ -28,12 +43,14 @@ impl Encode for AtomicU8 {
}
}
#[cfg(target_has_atomic = "8")]
impl Decode for AtomicU8 {
fn decode<D: crate::de::Decoder>(decoder: &mut D) -> Result<Self, crate::error::DecodeError> {
Ok(AtomicU8::new(Decode::decode(decoder)?))
}
}
#[cfg(target_has_atomic = "16")]
impl Encode for AtomicU16 {
fn encode<E: crate::enc::Encoder>(
&self,
@ -43,12 +60,14 @@ impl Encode for AtomicU16 {
}
}
#[cfg(target_has_atomic = "16")]
impl Decode for AtomicU16 {
fn decode<D: crate::de::Decoder>(decoder: &mut D) -> Result<Self, crate::error::DecodeError> {
Ok(AtomicU16::new(Decode::decode(decoder)?))
}
}
#[cfg(target_has_atomic = "32")]
impl Encode for AtomicU32 {
fn encode<E: crate::enc::Encoder>(
&self,
@ -58,12 +77,14 @@ impl Encode for AtomicU32 {
}
}
#[cfg(target_has_atomic = "32")]
impl Decode for AtomicU32 {
fn decode<D: crate::de::Decoder>(decoder: &mut D) -> Result<Self, crate::error::DecodeError> {
Ok(AtomicU32::new(Decode::decode(decoder)?))
}
}
#[cfg(target_has_atomic = "64")]
impl Encode for AtomicU64 {
fn encode<E: crate::enc::Encoder>(
&self,
@ -73,12 +94,14 @@ impl Encode for AtomicU64 {
}
}
#[cfg(target_has_atomic = "64")]
impl Decode for AtomicU64 {
fn decode<D: crate::de::Decoder>(decoder: &mut D) -> Result<Self, crate::error::DecodeError> {
Ok(AtomicU64::new(Decode::decode(decoder)?))
}
}
#[cfg(target_has_atomic = "ptr")]
impl Encode for AtomicUsize {
fn encode<E: crate::enc::Encoder>(
&self,
@ -88,12 +111,14 @@ impl Encode for AtomicUsize {
}
}
#[cfg(target_has_atomic = "ptr")]
impl Decode for AtomicUsize {
fn decode<D: crate::de::Decoder>(decoder: &mut D) -> Result<Self, crate::error::DecodeError> {
Ok(AtomicUsize::new(Decode::decode(decoder)?))
}
}
#[cfg(target_has_atomic = "8")]
impl Encode for AtomicI8 {
fn encode<E: crate::enc::Encoder>(
&self,
@ -103,12 +128,14 @@ impl Encode for AtomicI8 {
}
}
#[cfg(target_has_atomic = "8")]
impl Decode for AtomicI8 {
fn decode<D: crate::de::Decoder>(decoder: &mut D) -> Result<Self, crate::error::DecodeError> {
Ok(AtomicI8::new(Decode::decode(decoder)?))
}
}
#[cfg(target_has_atomic = "16")]
impl Encode for AtomicI16 {
fn encode<E: crate::enc::Encoder>(
&self,
@ -118,12 +145,14 @@ impl Encode for AtomicI16 {
}
}
#[cfg(target_has_atomic = "16")]
impl Decode for AtomicI16 {
fn decode<D: crate::de::Decoder>(decoder: &mut D) -> Result<Self, crate::error::DecodeError> {
Ok(AtomicI16::new(Decode::decode(decoder)?))
}
}
#[cfg(target_has_atomic = "32")]
impl Encode for AtomicI32 {
fn encode<E: crate::enc::Encoder>(
&self,
@ -133,12 +162,14 @@ impl Encode for AtomicI32 {
}
}
#[cfg(target_has_atomic = "32")]
impl Decode for AtomicI32 {
fn decode<D: crate::de::Decoder>(decoder: &mut D) -> Result<Self, crate::error::DecodeError> {
Ok(AtomicI32::new(Decode::decode(decoder)?))
}
}
#[cfg(target_has_atomic = "64")]
impl Encode for AtomicI64 {
fn encode<E: crate::enc::Encoder>(
&self,
@ -148,12 +179,14 @@ impl Encode for AtomicI64 {
}
}
#[cfg(target_has_atomic = "64")]
impl Decode for AtomicI64 {
fn decode<D: crate::de::Decoder>(decoder: &mut D) -> Result<Self, crate::error::DecodeError> {
Ok(AtomicI64::new(Decode::decode(decoder)?))
}
}
#[cfg(target_has_atomic = "ptr")]
impl Encode for AtomicIsize {
fn encode<E: crate::enc::Encoder>(
&self,
@ -163,6 +196,7 @@ impl Encode for AtomicIsize {
}
}
#[cfg(target_has_atomic = "ptr")]
impl Decode for AtomicIsize {
fn decode<D: crate::de::Decoder>(decoder: &mut D) -> Result<Self, crate::error::DecodeError> {
Ok(AtomicIsize::new(Decode::decode(decoder)?))

View File

@ -1,8 +1,3 @@
#[cfg(feature = "atomic")]
mod atomic;
#[cfg(feature = "atomic")]
pub use self::atomic::*;
#[cfg(feature = "alloc")]
mod impl_alloc;
#[cfg(feature = "alloc")]

View File

@ -80,6 +80,7 @@ extern crate alloc;
#[cfg(any(feature = "std", test))]
extern crate std;
mod atomic;
mod features;
pub(crate) mod utils;
pub(crate) mod varint;
@ -93,6 +94,7 @@ pub mod de;
pub mod enc;
pub mod error;
pub use atomic::*;
pub use de::{BorrowDecode, Decode};
pub use enc::Encode;

View File

@ -104,7 +104,10 @@ fn test_container_limits() {
// for this test we'll create a malformed package of a lot of bytes
let test_cases = &[
// u64::max_value(), should overflow
#[cfg(target_pointer_width = "64")]
bincode::encode_to_vec(u64::max_value(), bincode::config::standard()).unwrap(),
#[cfg(target_pointer_width = "32")]
bincode::encode_to_vec(u32::max_value(), bincode::config::standard()).unwrap(),
// A high value which doesn't overflow, but exceeds the decode limit
bincode::encode_to_vec(DECODE_LIMIT as u64, bincode::config::standard()).unwrap(),
];