Skip to content

Commit

Permalink
Update rustfmt.toml and format
Browse files Browse the repository at this point in the history
  • Loading branch information
djkoloski committed Aug 10, 2024
1 parent d94da6c commit cafccd0
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 52 deletions.
98 changes: 52 additions & 46 deletions bytecheck/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
//! ## Examples
//!
//! ```
//! use bytecheck::{CheckBytes, check_bytes, rancor::Failure};
//! use bytecheck::{check_bytes, rancor::Failure, CheckBytes};
//!
//! #[derive(CheckBytes, Debug)]
//! #[repr(C)]
Expand Down Expand Up @@ -56,52 +56,58 @@
//! // These are valid bytes for a `Test`
//! check_bytes::<Test, Failure>(
//! bytes![
//! 0u8, 0u8, 0u8, 0u8,
//! 0x78u8, 0u8, 0u8, 0u8,
//! 1u8, 255u8, 255u8, 255u8,
//! ].cast()
//! ).unwrap();
//! 0u8, 0u8, 0u8, 0u8, 0x78u8, 0u8, 0u8, 0u8, 1u8, 255u8, 255u8,
//! 255u8,
//! ]
//! .cast(),
//! )
//! .unwrap();
//!
//! // Changing the bytes for the u32 is OK, any bytes are a valid u32
//! check_bytes::<Test, Failure>(
//! bytes![
//! 42u8, 16u8, 20u8, 3u8,
//! 0x78u8, 0u8, 0u8, 0u8,
//! 1u8, 255u8, 255u8, 255u8,
//! ].cast()
//! ).unwrap();
//! 42u8, 16u8, 20u8, 3u8, 0x78u8, 0u8, 0u8, 0u8, 1u8, 255u8,
//! 255u8, 255u8,
//! ]
//! .cast(),
//! )
//! .unwrap();
//!
//! // Characters outside the valid ranges are invalid
//! check_bytes::<Test, Failure>(
//! bytes![
//! 0u8, 0u8, 0u8, 0u8,
//! 0x00u8, 0xd8u8, 0u8, 0u8,
//! 1u8, 255u8, 255u8, 255u8,
//! ].cast()
//! ).unwrap_err();
//! 0u8, 0u8, 0u8, 0u8, 0x00u8, 0xd8u8, 0u8, 0u8, 1u8, 255u8,
//! 255u8, 255u8,
//! ]
//! .cast(),
//! )
//! .unwrap_err();
//! check_bytes::<Test, Failure>(
//! bytes![
//! 0u8, 0u8, 0u8, 0u8,
//! 0x00u8, 0x00u8, 0x11u8, 0u8,
//! 1u8, 255u8, 255u8, 255u8,
//! ].cast()
//! ).unwrap_err();
//! 0u8, 0u8, 0u8, 0u8, 0x00u8, 0x00u8, 0x11u8, 0u8, 1u8, 255u8,
//! 255u8, 255u8,
//! ]
//! .cast(),
//! )
//! .unwrap_err();
//!
//! // 0 is a valid boolean value (false) but 2 is not
//! check_bytes::<Test, Failure>(
//! bytes![
//! 0u8, 0u8, 0u8, 0u8,
//! 0x78u8, 0u8, 0u8, 0u8,
//! 0u8, 255u8, 255u8, 255u8,
//! ].cast()
//! ).unwrap();
//! 0u8, 0u8, 0u8, 0u8, 0x78u8, 0u8, 0u8, 0u8, 0u8, 255u8, 255u8,
//! 255u8,
//! ]
//! .cast(),
//! )
//! .unwrap();
//! check_bytes::<Test, Failure>(
//! bytes![
//! 0u8, 0u8, 0u8, 0u8,
//! 0x78u8, 0u8, 0u8, 0u8,
//! 2u8, 255u8, 255u8, 255u8,
//! ].cast()
//! ).unwrap_err();
//! 0u8, 0u8, 0u8, 0u8, 0x78u8, 0u8, 0u8, 0u8, 2u8, 255u8, 255u8,
//! 255u8,
//! ]
//! .cast(),
//! )
//! .unwrap_err();
//! }
//! ```
//!
Expand Down Expand Up @@ -169,12 +175,12 @@ use core::{
},
ops, ptr,
};
use rancor::{fail, Fallible, ResultExt as _, Source, Strategy, Trace};
#[cfg(feature = "simdutf8")]
use simdutf8::basic::from_utf8;

pub use bytecheck_derive::CheckBytes;
pub use rancor;
use rancor::{fail, Fallible, ResultExt as _, Source, Strategy, Trace};
#[cfg(feature = "simdutf8")]
use simdutf8::basic::from_utf8;

/// A type that can check whether a pointer points to a valid value.
///
Expand Down Expand Up @@ -318,11 +324,11 @@ where
value: *const Self,
c: &mut C,
) -> Result<(), C::Error> {
// SAFETY: Because `ManuallyDrop<T>` is `#[repr(transparent)]`, a
// pointer to a `ManuallyDrop<T>` is guaranteed to be the same as a
// pointer to `T`. We can't call `.cast()` here because `T` may be
// an unsized type.
let inner_ptr =
// SAFETY: Because `ManuallyDrop<T>` is `#[repr(transparent)]`, a
// pointer to a `ManuallyDrop<T>` is guaranteed to be the same as a
// pointer to `T`. We can't call `.cast()` here because `T` may be
// an unsized type.
unsafe { core::mem::transmute::<*const Self, *const T>(value) };
// SAFETY: The caller has guaranteed that `value` is aligned for
// `ManuallyDrop<T>` and points to enough bytes to represent
Expand All @@ -349,11 +355,11 @@ where
value: *const Self,
c: &mut C,
) -> Result<(), C::Error> {
// SAFETY: Because `UnsafeCell<T>` has the same memory layout as
// `T`, a pointer to an `UnsafeCell<T>` is guaranteed to be the same
// as a pointer to `T`. We can't call `.cast()` here because `T` may
// be an unsized type.
let inner_ptr =
// SAFETY: Because `UnsafeCell<T>` has the same memory layout as
// `T`, a pointer to an `UnsafeCell<T>` is guaranteed to be the same
// as a pointer to `T`. We can't call `.cast()` here because `T` may
// be an unsized type.
unsafe { core::mem::transmute::<*const Self, *const T>(value) };
// SAFETY: The caller has guaranteed that `value` is aligned for
// `UnsafeCell<T>` and points to enough bytes to represent
Expand Down Expand Up @@ -381,11 +387,11 @@ where
value: *const Self,
c: &mut C,
) -> Result<(), C::Error> {
// SAFETY: Because `Cell<T>` has the same memory layout as
// `UnsafeCell<T>` (and therefore `T` itself), a pointer to a
// `Cell<T>` is guaranteed to be the same as a pointer to `T`. We
// can't call `.cast()` here because `T` may be an unsized type.
let inner_ptr =
// SAFETY: Because `Cell<T>` has the same memory layout as
// `UnsafeCell<T>` (and therefore `T` itself), a pointer to a
// `Cell<T>` is guaranteed to be the same as a pointer to `T`. We
// can't call `.cast()` here because `T` may be an unsized type.
unsafe { core::mem::transmute::<*const Self, *const T>(value) };
// SAFETY: The caller has guaranteed that `value` is aligned for
// `Cell<T>` and points to enough bytes to represent `Cell<T>`. Since
Expand Down
14 changes: 8 additions & 6 deletions bytecheck_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,14 @@ mod repr;

use proc_macro2::TokenStream;
use quote::{quote, ToTokens};
use repr::Repr;
use syn::{
meta::ParseNestedMeta, parenthesized, parse::Parse, parse_macro_input,
parse_quote, punctuated::Punctuated, spanned::Spanned, AttrStyle, Data,
DeriveInput, Error, Field, Fields, Ident, Index, LitStr, Path, Token,
WherePredicate,
};

use repr::Repr;

use crate::repr::BaseRepr;

#[derive(Default)]
Expand Down Expand Up @@ -338,10 +337,13 @@ fn derive_check_bytes(mut input: DeriveInput) -> Result<TokenStream, Error> {
},
Data::Enum(ref data) => {
let repr = match attributes.repr.base_repr {
None => return Err(Error::new_spanned(
name,
"enums implementing CheckBytes must have an explicit repr",
)),
None => {
return Err(Error::new_spanned(
name,
"enums implementing CheckBytes must have an explicit \
repr",
))
}
Some((BaseRepr::Transparent, _)) => {
return Err(Error::new_spanned(
name,
Expand Down
14 changes: 14 additions & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -1 +1,15 @@
max_width = 80
comment_width = 80
wrap_comments = true
group_imports = "StdExternalCrate"
imports_granularity = "Crate"
condense_wildcard_suffixes = true
error_on_line_overflow = true
error_on_unformatted = true
format_code_in_doc_comments = true
format_macro_matchers = true
format_macro_bodies = true
format_strings = true
hex_literal_case = "Lower"
normalize_comments = true
use_field_init_shorthand = true

0 comments on commit cafccd0

Please sign in to comment.