Skip to content

Commit d1993c5

Browse files
GnomedDevreitermarkus
authored andcommitted
Add generic LenType to String
1 parent 4e38494 commit d1993c5

File tree

9 files changed

+114
-99
lines changed

9 files changed

+114
-99
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
5353
- Added the `index_set` module.
5454
- Added the `index_map` module.
5555
- Migrated `Idx` generic for `SortedLinkedList` to use the new `LenType` trait, allowing for `Idx` inference.
56+
- Added similar `LenT` generic to `String`.
5657

5758
### Changed
5859

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/len_type.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ pub trait SmallestLenType {
108108
/// then map from that to an unrelated type via a trait with associated types.
109109
///
110110
/// [`Const`] is the "related type" in the above explaination, [`SmallestLenType`] is the mapping trait.
111+
#[allow(rustdoc::private_intra_doc_links)] // Only publically exposed via `crate::_export`
111112
pub type DefaultLenType<const N: usize> = <Const<N> as SmallestLenType>::Type;
112113

113114
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);

src/lib.rs

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

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: 20 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,42 @@ 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+
let start = self.start.into_usize();
40+
let end = self.end.into_usize();
41+
if start <= end && end <= self_vec.len() {
42+
self_vec.drain(start..end);
3943
}
4044
}
4145
}
4246
}
4347

44-
impl Drain<'_> {
48+
impl<LenT: LenType> Drain<'_, LenT> {
4549
/// Returns the remaining (sub)string of this iterator as a slice.
4650
///
4751
/// # Examples
@@ -61,19 +65,19 @@ impl Drain<'_> {
6165
}
6266
}
6367

64-
impl AsRef<str> for Drain<'_> {
68+
impl<LenT: LenType> AsRef<str> for Drain<'_, LenT> {
6569
fn as_ref(&self) -> &str {
6670
self.as_str()
6771
}
6872
}
6973

70-
impl AsRef<[u8]> for Drain<'_> {
74+
impl<LenT: LenType> AsRef<[u8]> for Drain<'_, LenT> {
7175
fn as_ref(&self) -> &[u8] {
7276
self.as_str().as_bytes()
7377
}
7478
}
7579

76-
impl Iterator for Drain<'_> {
80+
impl<LenT: LenType> Iterator for Drain<'_, LenT> {
7781
type Item = char;
7882

7983
#[inline]
@@ -91,14 +95,14 @@ impl Iterator for Drain<'_> {
9195
}
9296
}
9397

94-
impl DoubleEndedIterator for Drain<'_> {
98+
impl<LenT: LenType> DoubleEndedIterator for Drain<'_, LenT> {
9599
#[inline]
96100
fn next_back(&mut self) -> Option<char> {
97101
self.iter.next_back()
98102
}
99103
}
100104

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

103107
#[cfg(test)]
104108
mod tests {

0 commit comments

Comments
 (0)