Skip to content

Commit e43a751

Browse files
authored
Merge pull request #487 from Nitrokey/string-view-dedup
String: implement `StringView` on top of #486
2 parents 9abec11 + c513d2d commit e43a751

File tree

6 files changed

+215
-113
lines changed

6 files changed

+215
-113
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
2525
- Added `String::drain`.
2626
- Implemented `DoubleEndedIterator` for `OldestOrdered`.
2727
- Added std `Entry` methods to indexmap `Entry`.
28+
- Added `StringView`, the `!Sized` version of `String`.
2829

2930
### Changed
3031

src/defmt.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Defmt implementations for heapless types
22
3-
use crate::{storage::Storage, vec::VecInner};
3+
use crate::{storage::Storage, string::StringInner, vec::VecInner};
44
use defmt::Formatter;
55

66
impl<T, S: Storage> defmt::Format for VecInner<T, S>
@@ -12,7 +12,7 @@ where
1212
}
1313
}
1414

15-
impl<const N: usize> defmt::Format for crate::String<N>
15+
impl<S: Storage> defmt::Format for StringInner<S>
1616
where
1717
u8: defmt::Format,
1818
{

src/ser.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use core::hash::{BuildHasher, Hash};
22

33
use crate::{
4-
binary_heap::Kind as BinaryHeapKind, storage::Storage, vec::VecInner, BinaryHeap, Deque,
5-
HistoryBuffer, IndexMap, IndexSet, LinearMap, String,
4+
binary_heap::Kind as BinaryHeapKind, storage::Storage, string::StringInner, vec::VecInner,
5+
BinaryHeap, Deque, HistoryBuffer, IndexMap, IndexSet, LinearMap,
66
};
77
use serde::ser::{Serialize, SerializeMap, SerializeSeq, Serializer};
88

@@ -129,10 +129,10 @@ where
129129

130130
// String containers
131131

132-
impl<const N: usize> Serialize for String<N> {
133-
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
132+
impl<S: Storage> Serialize for StringInner<S> {
133+
fn serialize<SER>(&self, serializer: SER) -> Result<SER::Ok, SER::Error>
134134
where
135-
S: Serializer,
135+
SER: Serializer,
136136
{
137137
serializer.serialize_str(&*self)
138138
}

src/string/drain.rs

+17-17
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,34 @@
11
use core::{fmt, iter::FusedIterator, str::Chars};
22

3-
use super::String;
3+
use super::StringView;
44

55
/// A draining iterator for `String`.
66
///
7-
/// This struct is created by the [`drain`] method on [`String`]. See its
7+
/// This struct is created by the [`drain`] method on [`crate::String`]. See its
88
/// documentation for more.
99
///
10-
/// [`drain`]: String::drain
11-
pub struct Drain<'a, const N: usize> {
10+
/// [`drain`]: crate::String::drain
11+
pub struct Drain<'a> {
1212
/// Will be used as &'a mut String in the destructor
13-
pub(super) string: *mut String<N>,
14-
/// Start of part to remove
13+
pub(super) string: *mut StringView,
14+
/// Stast of part to remove
1515
pub(super) start: usize,
1616
/// End of part to remove
1717
pub(super) end: usize,
1818
/// Current remaining range to remove
1919
pub(super) iter: Chars<'a>,
2020
}
2121

22-
impl<const N: usize> fmt::Debug for Drain<'_, N> {
22+
impl fmt::Debug for Drain<'_> {
2323
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2424
f.debug_tuple("Drain").field(&self.as_str()).finish()
2525
}
2626
}
2727

28-
unsafe impl<const N: usize> Sync for Drain<'_, N> {}
29-
unsafe impl<const N: usize> Send for Drain<'_, N> {}
28+
unsafe impl Sync for Drain<'_> {}
29+
unsafe impl Send for Drain<'_> {}
3030

31-
impl<const N: usize> Drop for Drain<'_, N> {
31+
impl Drop for Drain<'_> {
3232
fn drop(&mut self) {
3333
unsafe {
3434
// Use `Vec::drain`. “Reaffirm” the bounds checks to avoid
@@ -41,7 +41,7 @@ impl<const N: usize> Drop for Drain<'_, N> {
4141
}
4242
}
4343

44-
impl<'a, const N: usize> Drain<'a, N> {
44+
impl<'a> Drain<'a> {
4545
/// Returns the remaining (sub)string of this iterator as a slice.
4646
///
4747
/// # Examples
@@ -61,19 +61,19 @@ impl<'a, const N: usize> Drain<'a, N> {
6161
}
6262
}
6363

64-
impl<const N: usize> AsRef<str> for Drain<'_, N> {
64+
impl AsRef<str> for Drain<'_> {
6565
fn as_ref(&self) -> &str {
6666
self.as_str()
6767
}
6868
}
6969

70-
impl<const N: usize> AsRef<[u8]> for Drain<'_, N> {
70+
impl AsRef<[u8]> for Drain<'_> {
7171
fn as_ref(&self) -> &[u8] {
7272
self.as_str().as_bytes()
7373
}
7474
}
7575

76-
impl<const N: usize> Iterator for Drain<'_, N> {
76+
impl Iterator for Drain<'_> {
7777
type Item = char;
7878

7979
#[inline]
@@ -91,18 +91,18 @@ impl<const N: usize> Iterator for Drain<'_, N> {
9191
}
9292
}
9393

94-
impl<const N: usize> DoubleEndedIterator for Drain<'_, N> {
94+
impl DoubleEndedIterator for Drain<'_> {
9595
#[inline]
9696
fn next_back(&mut self) -> Option<char> {
9797
self.iter.next_back()
9898
}
9999
}
100100

101-
impl<const N: usize> FusedIterator for Drain<'_, N> {}
101+
impl FusedIterator for Drain<'_> {}
102102

103103
#[cfg(test)]
104104
mod tests {
105-
use super::String;
105+
use crate::String;
106106

107107
#[test]
108108
fn drain_front() {

0 commit comments

Comments
 (0)