Skip to content

Commit

Permalink
Release 0.5.0-pre3
Browse files Browse the repository at this point in the history
  • Loading branch information
djkoloski committed Dec 12, 2023
1 parent 9c6a994 commit 0b81042
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 29 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rend"
version = "0.5.0-pre2"
version = "0.5.0-pre3"
authors = ["David Koloski <djkoloski@gmail.com>"]
edition = "2021"
description = "Endian-aware primitives for Rust"
Expand All @@ -14,7 +14,7 @@ readme = "crates-io.md"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
bytecheck = { version = "0.8.0-pre1", optional = true, default-features = false }
bytecheck = { version = "0.8.0-pre3", optional = true, default-features = false }

[features]
default = []
40 changes: 18 additions & 22 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,8 @@ macro_rules! impl_signed_integer_traits {
impl_unop!(Neg::neg for $name: $prim);
impl_unop!(Not::not for $name: $prim);
impl_fmt!(Octal for $name);
impl_ord!(for $name);
impl_partial_eq_and_eq!(for $name: $prim);
impl_partial_ord!(for $name: $prim);
impl_partial_ord_and_ord!(for $name: $prim);
impl_product_and_sum!(for $name);
impl_binop!(Rem::rem for $name: $prim);
impl_binassign!(RemAssign::rem_assign for $name: $prim);
Expand Down Expand Up @@ -100,9 +99,8 @@ macro_rules! impl_unsigned_integer_traits {
impl_binassign!(MulAssign::mul_assign for $name: $prim);
impl_unop!(Not::not for $name: $prim);
impl_fmt!(Octal for $name);
impl_ord!(for $name);
impl_partial_eq_and_eq!(for $name: $prim);
impl_partial_ord!(for $name: $prim);
impl_partial_ord_and_ord!(for $name: $prim);
impl_product_and_sum!(for $name);
impl_binop!(Rem::rem for $name: $prim);
impl_binassign!(RemAssign::rem_assign for $name: $prim);
Expand Down Expand Up @@ -230,25 +228,24 @@ macro_rules! impl_char {
impl_fmt!(Display for $name);
impl_from!(for $name: char);
impl_hash!(for $name);
impl_ord!(for $name);
impl_partial_eq_and_eq!(for $name: char);
impl_partial_ord!(for $name: char);
impl_partial_ord_and_ord!(for $name: char);

#[cfg(feature = "bytecheck")]
// SAFETY: `check_bytes` only returns `Ok` if the code point contained
// within the endian-aware `char` represents a valid `char`.
unsafe impl<C, E> bytecheck::CheckBytes<C, E> for $name
unsafe impl<C> bytecheck::CheckBytes<C> for $name
where
C: ?Sized,
E: bytecheck::rancor::Contextual,
char: bytecheck::CheckBytes<C, E>,
C: bytecheck::rancor::Fallible + ?Sized,
C::Error: bytecheck::rancor::Trace,
char: bytecheck::CheckBytes<C>,
{
#[inline]
unsafe fn check_bytes(
value: *const Self,
context: &mut C,
) -> Result<(), E> {
use bytecheck::rancor::Context;
) -> Result<(), C::Error> {
use bytecheck::rancor::ResultExt as _;

// SAFETY: `value` points to a `Self`, which has the same size
// as a `u32` and is at least as aligned as one.
Expand All @@ -258,7 +255,7 @@ macro_rules! impl_char {
// `c` is a valid `char`.
unsafe {
char::check_bytes(&c as *const u32 as *const char, context)
.with_context(|| $crate::context::ValueCheckContext {
.with_trace(|| $crate::context::ValueCheckContext {
inner_name: "char",
outer_name: core::stringify!($name),
})
Expand Down Expand Up @@ -337,34 +334,33 @@ macro_rules! impl_nonzero {
impl_hash!(for $name);
impl_fmt!(LowerHex for $name);
impl_fmt!(Octal for $name);
impl_ord!(for $name);
impl_partial_eq_and_eq!(for $name: $prim);
impl_partial_ord!(for $name: $prim);
impl_partial_ord_and_ord!(for $name: $prim);
impl_fmt!(UpperHex for $name);

#[cfg(feature = "bytecheck")]
// SAFETY: `check_bytes` only returns `Ok` if `value` points to a valid
// non-zero value, which is the only requirement for `NonZero` integers.
unsafe impl<C, E> bytecheck::CheckBytes<C, E> for $name
unsafe impl<C> bytecheck::CheckBytes<C> for $name
where
C: ?Sized,
E: bytecheck::rancor::Contextual,
$prim: bytecheck::CheckBytes<C, E>,
C: bytecheck::rancor::Fallible + ?Sized,
C::Error: bytecheck::rancor::Trace,
$prim: bytecheck::CheckBytes<C>,
{
#[inline]
unsafe fn check_bytes(
value: *const Self,
context: &mut C,
) -> Result<(), E> {
use bytecheck::rancor::Context;
) -> Result<(), C::Error> {
use bytecheck::rancor::ResultExt as _;

// SAFETY: `value` points to a `Self`, which has the same size
// as a `$prim` and is at least as aligned as one. Note that the
// bit pattern for 0 is always the same regardless of
// endianness.
unsafe {
<$prim>::check_bytes(value.cast(), context)
.with_context(|| $crate::context::ValueCheckContext {
.with_trace(|| $crate::context::ValueCheckContext {
inner_name: core::stringify!($prim),
outer_name: core::stringify!($name),
})
Expand Down
36 changes: 31 additions & 5 deletions src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ macro_rules! impl_clone_and_copy {
impl Clone for $name {
#[inline]
fn clone(&self) -> Self {
Self(self.0)
*self
}
}

Expand Down Expand Up @@ -210,8 +210,28 @@ macro_rules! impl_hash {
};
}

macro_rules! impl_ord {
(for $name:ident) => {
macro_rules! impl_partial_ord_and_ord {
(for $name:ident: $prim:ty) => {
impl PartialOrd for $name {
#[inline]
fn partial_cmp(
&self,
other: &Self,
) -> Option<::core::cmp::Ordering> {
Some(self.cmp(other))
}
}

impl PartialOrd<$prim> for $name {
#[inline]
fn partial_cmp(
&self,
other: &$prim,
) -> Option<::core::cmp::Ordering> {
self.to_native().partial_cmp(other)
}
}

impl Ord for $name {
#[inline]
fn cmp(&self, other: &Self) -> ::core::cmp::Ordering {
Expand Down Expand Up @@ -302,9 +322,15 @@ macro_rules! unsafe_impl_check_bytes_noop {
// SAFETY: All callers of this macro have guaranteed that all pointers
// to `$name`s which are properly aligned and point to enough bytes to
// represent the type also point to a valid instance of the type.
unsafe impl<C: ?Sized, E> bytecheck::CheckBytes<C, E> for $name {
unsafe impl<C> bytecheck::CheckBytes<C> for $name
where
C: bytecheck::rancor::Fallible + ?Sized,
{
#[inline]
unsafe fn check_bytes(_: *const Self, _: &mut C) -> Result<(), E> {
unsafe fn check_bytes(
_: *const Self,
_: &mut C,
) -> Result<(), C::Error> {
// SAFETY: The invoker of this macro has guaranteed that an impl
// of `CheckBytes` with a `check_bytes` function that is a no-op
// is sound.
Expand Down

0 comments on commit 0b81042

Please sign in to comment.