Skip to content

Commit b412973

Browse files
committed
Add generic LenType to String
1 parent fb62d12 commit b412973

File tree

9 files changed

+116
-99
lines changed

9 files changed

+116
-99
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
4949
- Added `get_index` and `get_index_mut` to `IndexMap`.
5050
- Added `String::uDisplay`.
5151
- Added `LenT` generic to `Vec<T, N>` and `VecView<T>` to save memory when using a sane capacity value.
52+
- Added similar `LenT` generic to `String`.
5253

5354
### Changed
5455

src/de.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -299,15 +299,15 @@ where
299299

300300
// String containers
301301

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

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

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

342-
deserializer.deserialize_str(ValueVisitor::<'de, N>(PhantomData))
342+
deserializer.deserialize_str(ValueVisitor(PhantomData))
343343
}
344344
}

src/defmt.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,11 @@ where
1212
T: defmt::Format,
1313
{
1414
fn format(&self, fmt: Formatter<'_>) {
15-
defmt::write!(fmt, "{=[?]}", self.as_slice())
15+
defmt::write!(fmt, "{=[?]}", self.as_slice());
1616
}
1717
}
1818

19-
impl<S: StringStorage + ?Sized> defmt::Format for StringInner<S>
20-
where
21-
u8: defmt::Format,
22-
{
19+
impl<LenT: LenType, S: StringStorage + ?Sized> defmt::Format for StringInner<LenT, S> {
2320
fn format(&self, fmt: Formatter<'_>) {
2421
defmt::write!(fmt, "{=str}", self.as_str());
2522
}

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ mod ufmt;
243243
/// Do not use. Used for macros only. Not covered by semver guarantees.
244244
#[doc(hidden)]
245245
pub mod _export {
246+
pub use crate::len_type::DefaultLenType;
246247
pub use crate::string::format;
247248
}
248249

src/ser.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,11 +136,11 @@ where
136136

137137
// String containers
138138

139-
impl<S: StringStorage + ?Sized> Serialize for StringInner<S> {
139+
impl<LenT: LenType, S: StringStorage + ?Sized> Serialize for StringInner<LenT, S> {
140140
fn serialize<SER>(&self, serializer: SER) -> Result<SER::Ok, SER::Error>
141141
where
142142
SER: Serializer,
143143
{
144-
serializer.serialize_str(&*self)
144+
serializer.serialize_str(self)
145145
}
146146
}

src/string/drain.rs

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use core::{fmt, iter::FusedIterator, str::Chars};
22

3+
use crate::LenType;
4+
35
use super::StringView;
46

57
/// A draining iterator for `String`.
@@ -8,40 +10,40 @@ use super::StringView;
810
/// documentation for more.
911
///
1012
/// [`drain`]: crate::String::drain
11-
pub struct Drain<'a> {
13+
pub struct Drain<'a, LenT: LenType> {
1214
/// Will be used as &'a mut String in the destructor
13-
pub(super) string: *mut StringView,
15+
pub(super) string: *mut StringView<LenT>,
1416
/// Stast of part to remove
15-
pub(super) start: usize,
17+
pub(super) start: LenT,
1618
/// End of part to remove
17-
pub(super) end: usize,
19+
pub(super) end: LenT,
1820
/// Current remaining range to remove
1921
pub(super) iter: Chars<'a>,
2022
}
2123

22-
impl fmt::Debug for Drain<'_> {
24+
impl<LenT: LenType> fmt::Debug for Drain<'_, LenT> {
2325
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2426
f.debug_tuple("Drain").field(&self.as_str()).finish()
2527
}
2628
}
2729

28-
unsafe impl Sync for Drain<'_> {}
29-
unsafe impl Send for Drain<'_> {}
30+
unsafe impl<LenT: LenType> Sync for Drain<'_, LenT> {}
31+
unsafe impl<LenT: LenType> Send for Drain<'_, LenT> {}
3032

31-
impl Drop for Drain<'_> {
33+
impl<LenT: LenType> Drop for Drain<'_, LenT> {
3234
fn drop(&mut self) {
3335
unsafe {
3436
// Use `Vec::drain`. “Reaffirm” the bounds checks to avoid
3537
// panic code being inserted again.
3638
let self_vec = (*self.string).as_mut_vec();
37-
if self.start <= self.end && self.end <= self_vec.len() {
38-
self_vec.drain(self.start..self.end);
39+
if self.start <= self.end && self.end <= self_vec.length() {
40+
self_vec.drain(self.start.into_usize()..self.end.into_usize());
3941
}
4042
}
4143
}
4244
}
4345

44-
impl Drain<'_> {
46+
impl<LenT: LenType> Drain<'_, LenT> {
4547
/// Returns the remaining (sub)string of this iterator as a slice.
4648
///
4749
/// # Examples
@@ -61,19 +63,19 @@ impl Drain<'_> {
6163
}
6264
}
6365

64-
impl AsRef<str> for Drain<'_> {
66+
impl<LenT: LenType> AsRef<str> for Drain<'_, LenT> {
6567
fn as_ref(&self) -> &str {
6668
self.as_str()
6769
}
6870
}
6971

70-
impl AsRef<[u8]> for Drain<'_> {
72+
impl<LenT: LenType> AsRef<[u8]> for Drain<'_, LenT> {
7173
fn as_ref(&self) -> &[u8] {
7274
self.as_str().as_bytes()
7375
}
7476
}
7577

76-
impl Iterator for Drain<'_> {
78+
impl<LenT: LenType> Iterator for Drain<'_, LenT> {
7779
type Item = char;
7880

7981
#[inline]
@@ -91,14 +93,14 @@ impl Iterator for Drain<'_> {
9193
}
9294
}
9395

94-
impl DoubleEndedIterator for Drain<'_> {
96+
impl<LenT: LenType> DoubleEndedIterator for Drain<'_, LenT> {
9597
#[inline]
9698
fn next_back(&mut self) -> Option<char> {
9799
self.iter.next_back()
98100
}
99101
}
100102

101-
impl FusedIterator for Drain<'_> {}
103+
impl<LenT: LenType> FusedIterator for Drain<'_, LenT> {}
102104

103105
#[cfg(test)]
104106
mod tests {

0 commit comments

Comments
 (0)