Skip to content
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
4 changes: 4 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ on:
branches:
- main

pull_request:
branches:
- main

jobs:
build:
runs-on: ubuntu-latest
Expand Down
24 changes: 24 additions & 0 deletions src/alignment.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/// Zero-sized type (ZST) to enforce 8-byte memory alignment
#[repr(align(8))]
#[derive(PartialEq, Eq, Copy, Clone, Debug)]
pub struct Align8;

/// Zero-sized type (ZST) to enforce 16-byte memory alignment
#[repr(align(16))]
#[derive(PartialEq, Eq, Copy, Clone, Debug)]
pub struct Align16;

/// Zero-sized type (ZST) to enforce 32-byte memory alignment
#[repr(align(32))]
#[derive(PartialEq, Eq, Copy, Clone, Debug)]
pub struct Align32;

/// Zero-sized type (ZST) to enforce 64-byte memory alignment
#[repr(align(64))]
#[derive(PartialEq, Eq, Copy, Clone, Debug)]
pub struct Align64;

/// Zero-sized type (ZST) to enforce 128-byte memory alignment
#[repr(align(128))]
#[derive(PartialEq, Eq, Copy, Clone, Debug)]
pub struct Align128;
32 changes: 32 additions & 0 deletions src/alignment_resolver.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use crate::{Align8, Align16, Align32, Align64, Align128};

/// Marker type for which [AlignmentForLength] instances are provided
pub type AlignmentMarker = ();

/// Associates a BStr length `N` with a suitable alignment
pub trait AlignmentForLength<const N: usize> {
type Output;
}

impl AlignmentForLength<7> for AlignmentMarker {
type Output = Align8;
}

impl AlignmentForLength<15> for AlignmentMarker {
type Output = Align16;
}

impl AlignmentForLength<31> for AlignmentMarker {
type Output = Align32;
}

impl AlignmentForLength<63> for AlignmentMarker {
type Output = Align64;
}

impl AlignmentForLength<127> for AlignmentMarker {
type Output = Align128;
}

/// Resolves the alignment type for the given BStr length `N`
pub type AlignmentType<const N: usize> = <AlignmentMarker as AlignmentForLength<N>>::Output;
29 changes: 18 additions & 11 deletions src/bounded_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::string::String;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

use crate::alignment_resolver::{AlignmentForLength, AlignmentMarker, AlignmentType};
use crate::bitmap_resolver::{BitmapForLength, BitmapMarker, BitmapType};
use crate::errors::ExceedsCapacity;
use crate::str_vec::StrVec;
Expand Down Expand Up @@ -43,24 +44,26 @@ use crate::FStr64;
///
/// See also: [BStr7], [BStr15], [BStr31], [BStr63], [BStr127]
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct BoundedStr<const N: usize> {
pub struct BoundedStr<const N: usize, Alignment> {
length: u8,
data: [u8; N],
align: [Alignment; 0],
}

impl<const N: usize> Default for BoundedStr<N> {
impl<const N: usize, Alignment> Default for BoundedStr<N, Alignment> {
fn default() -> Self {
Self::new()
}
}

impl<const N: usize> BoundedStr<N> {
impl<const N: usize, Alignment> BoundedStr<N, Alignment> {
/// Create an empty BoundedStr
#[inline]
pub const fn new() -> Self {
Self {
length: 0,
data: [0; N],
align: [],
}
}

Expand All @@ -86,6 +89,7 @@ impl<const N: usize> BoundedStr<N> {
BoundedStr {
length: length as u8,
data,
align: [],
}
}

Expand All @@ -108,6 +112,7 @@ impl<const N: usize> BoundedStr<N> {
Some(BoundedStr {
length: length as u8,
data,
align: [],
})
}
}
Expand All @@ -131,6 +136,7 @@ impl<const N: usize> BoundedStr<N> {
Ok(BoundedStr {
length: length as u8,
data,
align: [],
})
}

Expand Down Expand Up @@ -191,9 +197,10 @@ impl<const N: usize> BoundedStr<N> {
/// This function is only available for common N values (7, 15, 31 etc.) since
/// the corresponding bitmap size for StrVec is resolved at compile time using a
/// type-level mapping.
pub fn split(&self, delimiter: &str) -> StrVec<BitmapType<N>, N>
pub fn split(&self, delimiter: &str) -> StrVec<BitmapType<N>, N, AlignmentType<N>>
where
BitmapMarker: BitmapForLength<N>,
AlignmentMarker: AlignmentForLength<N>,
{
let s = self.as_str();

Expand All @@ -219,40 +226,40 @@ impl<const N: usize> BoundedStr<N> {
}
}

impl<const N: usize> fmt::Display for BoundedStr<N> {
impl<const N: usize, Alignment> fmt::Display for BoundedStr<N, Alignment> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}

impl<const N: usize> fmt::Debug for BoundedStr<N> {
impl<const N: usize, Alignment> fmt::Debug for BoundedStr<N, Alignment> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}

impl<const N: usize> From<&str> for BoundedStr<N> {
impl<const N: usize, Alignment> From<&str> for BoundedStr<N, Alignment> {
fn from(s: &str) -> Self {
Self::try_from(s).unwrap()
}
}

#[cfg(feature = "std")]
impl<const N: usize> From<&String> for BoundedStr<N> {
impl<const N: usize, Alignment> From<&String> for BoundedStr<N, Alignment> {
fn from(s: &String) -> Self {
Self::try_from(s).unwrap()
}
}

#[cfg(feature = "std")]
impl<const N: usize> From<String> for BoundedStr<N> {
impl<const N: usize, Alignment> From<String> for BoundedStr<N, Alignment> {
fn from(s: String) -> Self {
Self::try_from(&s).unwrap()
}
}

#[cfg(feature = "serde")]
impl<const N: usize> Serialize for BoundedStr<N> {
impl<const N: usize, Alignment> Serialize for BoundedStr<N, Alignment> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
Expand All @@ -262,7 +269,7 @@ impl<const N: usize> Serialize for BoundedStr<N> {
}

#[cfg(feature = "serde")]
impl<'de, const N: usize> Deserialize<'de> for BoundedStr<N> {
impl<'de, const N: usize, Alignment> Deserialize<'de> for BoundedStr<N, Alignment> {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
Expand Down
40 changes: 25 additions & 15 deletions src/fixed_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,27 @@ use crate::ExceedsCapacity;
/// # Aliases
/// See also: [FStr8], [FStr16], [FStr24], [FStr32], [FStr64], [FStr128]
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct FixedStr<const N: usize> {
pub struct FixedStr<const N: usize, Alignment> {
data: [u8; N],
align: [Alignment; 0],
}

impl<const N: usize> FixedStr<N> {
impl<const N: usize, Alignment> FixedStr<N, Alignment> {
/// Creates a NUL-padded FixedStr. Equivalent to FixedStr::default().
#[inline]
pub const fn new() -> Self {
FixedStr { data: [0; N] }
FixedStr {
data: [0; N],
align: [],
}
}

/// # Safety
/// This function requires that the provided bytes can be represented by a UTF-8 string.
/// Otherwise, [Self::as_str] and [Self::as_str_trimmed] are not well-defined.
#[inline]
pub const unsafe fn from_bytes(data: [u8; N]) -> Self {
FixedStr { data }
FixedStr { data, align: [] }
}

/// It is possible to construct a FixedStr shorter than its capacity, in which
Expand All @@ -76,7 +80,7 @@ impl<const N: usize> FixedStr<N> {
let bytes = s.as_bytes();
data[0..length].copy_from_slice(bytes);

Ok(FixedStr { data })
Ok(FixedStr { data, align: [] })
}

/// Builds FixedStr within a const context
Expand All @@ -89,7 +93,10 @@ impl<const N: usize> FixedStr<N> {

let bytes = s.as_bytes();

let mut t = FixedStr { data: [0; N] };
let mut t = FixedStr {
data: [0; N],
align: [],
};

{
let (left, _) = t.data.split_at_mut(length);
Expand All @@ -109,7 +116,10 @@ impl<const N: usize> FixedStr<N> {
} else {
let bytes = s.as_bytes();

let mut t = FixedStr { data: [0; N] };
let mut t = FixedStr {
data: [0; N],
align: [],
};

{
let (left, _) = t.data.split_at_mut(length);
Expand Down Expand Up @@ -155,46 +165,46 @@ impl<const N: usize> FixedStr<N> {
}
}

impl<const N: usize> Default for FixedStr<N> {
impl<const N: usize, Alignment> Default for FixedStr<N, Alignment> {
fn default() -> Self {
FixedStr::new()
}
}

impl<const N: usize> fmt::Display for FixedStr<N> {
impl<const N: usize, Alignment> fmt::Display for FixedStr<N, Alignment> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}

impl<const N: usize> fmt::Debug for FixedStr<N> {
impl<const N: usize, Alignment> fmt::Debug for FixedStr<N, Alignment> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str_trimmed())
}
}

impl<const N: usize> From<&str> for FixedStr<N> {
impl<const N: usize, Alignment> From<&str> for FixedStr<N, Alignment> {
fn from(s: &str) -> Self {
Self::try_from(s).unwrap()
}
}

#[cfg(feature = "std")]
impl<const N: usize> From<&String> for FixedStr<N> {
impl<const N: usize, Alignment> From<&String> for FixedStr<N, Alignment> {
fn from(s: &String) -> Self {
Self::try_from(s).unwrap()
}
}

#[cfg(feature = "std")]
impl<const N: usize> From<String> for FixedStr<N> {
impl<const N: usize, Alignment> From<String> for FixedStr<N, Alignment> {
fn from(s: String) -> Self {
Self::try_from(&s).unwrap()
}
}

#[cfg(feature = "serde")]
impl<const N: usize> Serialize for FixedStr<N> {
impl<const N: usize, Alignment> Serialize for FixedStr<N, Alignment> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
Expand All @@ -204,7 +214,7 @@ impl<const N: usize> Serialize for FixedStr<N> {
}

#[cfg(feature = "serde")]
impl<'de, const N: usize> Deserialize<'de> for FixedStr<N> {
impl<'de, const N: usize, Alignment> Deserialize<'de> for FixedStr<N, Alignment> {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
Expand Down
Loading
Loading