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

WIP: Add sized_str crate #6446

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Prev Previous commit
Next Next commit
Clippy
  • Loading branch information
rtfeldman committed Jan 28, 2024
commit 72e6c700bdb1891877e4d87687d588902c630544
10 changes: 7 additions & 3 deletions crates/sized_str/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 18,8 @@ macro_rules! make_str_n {
const NUM_LANES: usize = $num_lanes;
const ALIGN: usize = Self::NUM_LANES * std::mem::align_of::<$name>();

/// Safety: The given slice must:
/// # Safety
/// The given slice must:
/// - have a nonzero length that's a multiple of 4
/// - point to a memory address that's disible by 16
pub unsafe fn new_unchecked(slice: &'a [$name]) -> Self {
Expand All @@ -44,6 45,7 @@ macro_rules! make_str_n {
}

impl $name {
#[allow(clippy::len_without_is_empty)]
#[cfg(target_endian = "little")] // This implementation relies on little-endian byte ordering for the int
pub fn len(&self) -> usize {
// NonZeroU__::trailing_zeros compiles to a single instruction on x64, whereas
Expand Down Expand Up @@ -187,7 189,8 @@ impl Str4 {
/// (Other bytes past the first 4 are ignored.)
/// If there are fewer than 4 input bytes, pads the end with zeros internally.
///
/// Safety: There must be at least 8 bytes of safely accessible memory starting from the pointer.
/// # Safety
/// There must be at least 8 bytes of safely accessible memory starting from the pointer.
///
/// Note: These unusual API requirements are for performance; they avoid a memcpy call and branching!
pub unsafe fn from_raw_parts(input: *const NonZeroU8, len: NonZeroUsize) -> Self {
Expand Down Expand Up @@ -329,7 332,8 @@ impl Str8 {
/// (Other bytes past the first 8 are ignored.)
/// If there are fewer than 8 input bytes, pads the end with zeros internally.
///
/// Safety: There must be at least 8 bytes of safely accessible memory starting from the pointer.
/// # Safety
/// There must be at least 8 bytes of safely accessible memory starting from the pointer.
///
/// Note: These unusual API requirements are for performance; they avoid a memcpy call and branching!
pub unsafe fn from_raw_parts(input: *const NonZeroU8, len: NonZeroUsize) -> Self {
Expand Down
Loading