Skip to content

Add generic LenType to String #552

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

Merged
merged 1 commit into from
Apr 23, 2025
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Added the `index_set` module.
- Added the `index_map` module.
- Migrated `Idx` generic for `SortedLinkedList` to use the new `LenType` trait, allowing for `Idx` inference.
- Added similar `LenT` generic to `String`.

### Changed

Expand Down
10 changes: 5 additions & 5 deletions src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,15 +299,15 @@ where

// String containers

impl<'de, const N: usize> Deserialize<'de> for String<N> {
impl<'de, LenT: LenType, const N: usize> Deserialize<'de> for String<N, LenT> {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
struct ValueVisitor<'de, const N: usize>(PhantomData<&'de ()>);
struct ValueVisitor<'de, LenT: LenType, const N: usize>(PhantomData<(&'de (), LenT)>);

impl<'de, const N: usize> de::Visitor<'de> for ValueVisitor<'de, N> {
type Value = String<N>;
impl<'de, LenT: LenType, const N: usize> de::Visitor<'de> for ValueVisitor<'de, LenT, N> {
type Value = String<N, LenT>;

fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(formatter, "a string no more than {} bytes long", N as u64)
Expand Down Expand Up @@ -339,6 +339,6 @@ impl<'de, const N: usize> Deserialize<'de> for String<N> {
}
}

deserializer.deserialize_str(ValueVisitor::<'de, N>(PhantomData))
deserializer.deserialize_str(ValueVisitor(PhantomData))
}
}
7 changes: 2 additions & 5 deletions src/defmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,11 @@ where
T: defmt::Format,
{
fn format(&self, fmt: Formatter<'_>) {
defmt::write!(fmt, "{=[?]}", self.as_slice())
defmt::write!(fmt, "{=[?]}", self.as_slice());
}
}

impl<S: StringStorage + ?Sized> defmt::Format for StringInner<S>
where
u8: defmt::Format,
{
impl<LenT: LenType, S: StringStorage + ?Sized> defmt::Format for StringInner<LenT, S> {
fn format(&self, fmt: Formatter<'_>) {
defmt::write!(fmt, "{=str}", self.as_str());
}
Expand Down
1 change: 1 addition & 0 deletions src/len_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ pub trait SmallestLenType {
/// then map from that to an unrelated type via a trait with associated types.
///
/// [`Const`] is the "related type" in the above explaination, [`SmallestLenType`] is the mapping trait.
#[allow(rustdoc::private_intra_doc_links)] // Only publically exposed via `crate::_export`
pub type DefaultLenType<const N: usize> = <Const<N> as SmallestLenType>::Type;

impl_lentodefault!(u8: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255);
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ mod ufmt;
/// Do not use. Used for macros only. Not covered by semver guarantees.
#[doc(hidden)]
pub mod _export {
pub use crate::len_type::DefaultLenType;
pub use crate::string::format;
}

Expand Down
4 changes: 2 additions & 2 deletions src/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,11 @@ where

// String containers

impl<S: StringStorage + ?Sized> Serialize for StringInner<S> {
impl<LenT: LenType, S: StringStorage + ?Sized> Serialize for StringInner<LenT, S> {
fn serialize<SER>(&self, serializer: SER) -> Result<SER::Ok, SER::Error>
where
SER: Serializer,
{
serializer.serialize_str(&*self)
serializer.serialize_str(self)
}
}
36 changes: 20 additions & 16 deletions src/string/drain.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use core::{fmt, iter::FusedIterator, str::Chars};

use crate::LenType;

use super::StringView;

/// A draining iterator for `String`.
Expand All @@ -8,40 +10,42 @@ use super::StringView;
/// documentation for more.
///
/// [`drain`]: crate::String::drain
pub struct Drain<'a> {
pub struct Drain<'a, LenT: LenType> {
/// Will be used as &'a mut String in the destructor
pub(super) string: *mut StringView,
pub(super) string: *mut StringView<LenT>,
/// Stast of part to remove
pub(super) start: usize,
pub(super) start: LenT,
/// End of part to remove
pub(super) end: usize,
pub(super) end: LenT,
/// Current remaining range to remove
pub(super) iter: Chars<'a>,
}

impl fmt::Debug for Drain<'_> {
impl<LenT: LenType> fmt::Debug for Drain<'_, LenT> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("Drain").field(&self.as_str()).finish()
}
}

unsafe impl Sync for Drain<'_> {}
unsafe impl Send for Drain<'_> {}
unsafe impl<LenT: LenType> Sync for Drain<'_, LenT> {}
unsafe impl<LenT: LenType> Send for Drain<'_, LenT> {}

impl Drop for Drain<'_> {
impl<LenT: LenType> Drop for Drain<'_, LenT> {
fn drop(&mut self) {
unsafe {
// Use `Vec::drain`. “Reaffirm” the bounds checks to avoid
// panic code being inserted again.
let self_vec = (*self.string).as_mut_vec();
if self.start <= self.end && self.end <= self_vec.len() {
self_vec.drain(self.start..self.end);
let start = self.start.into_usize();
let end = self.end.into_usize();
if start <= end && end <= self_vec.len() {
self_vec.drain(start..end);
}
}
}
}

impl Drain<'_> {
impl<LenT: LenType> Drain<'_, LenT> {
/// Returns the remaining (sub)string of this iterator as a slice.
///
/// # Examples
Expand All @@ -61,19 +65,19 @@ impl Drain<'_> {
}
}

impl AsRef<str> for Drain<'_> {
impl<LenT: LenType> AsRef<str> for Drain<'_, LenT> {
fn as_ref(&self) -> &str {
self.as_str()
}
}

impl AsRef<[u8]> for Drain<'_> {
impl<LenT: LenType> AsRef<[u8]> for Drain<'_, LenT> {
fn as_ref(&self) -> &[u8] {
self.as_str().as_bytes()
}
}

impl Iterator for Drain<'_> {
impl<LenT: LenType> Iterator for Drain<'_, LenT> {
type Item = char;

#[inline]
Expand All @@ -91,14 +95,14 @@ impl Iterator for Drain<'_> {
}
}

impl DoubleEndedIterator for Drain<'_> {
impl<LenT: LenType> DoubleEndedIterator for Drain<'_, LenT> {
#[inline]
fn next_back(&mut self) -> Option<char> {
self.iter.next_back()
}
}

impl FusedIterator for Drain<'_> {}
impl<LenT: LenType> FusedIterator for Drain<'_, LenT> {}

#[cfg(test)]
mod tests {
Expand Down
Loading