Skip to content

Commit

Permalink
Merge pull request #487 from Nitrokey/string-view-dedup
Browse files Browse the repository at this point in the history
String: implement `StringView` on top of #486
  • Loading branch information
Dirbaio authored Jul 1, 2024
2 parents 9abec11 + c513d2d commit e43a751
Show file tree
Hide file tree
Showing 6 changed files with 215 additions and 113 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Added `String::drain`.
- Implemented `DoubleEndedIterator` for `OldestOrdered`.
- Added std `Entry` methods to indexmap `Entry`.
- Added `StringView`, the `!Sized` version of `String`.

### Changed

Expand Down
4 changes: 2 additions & 2 deletions src/defmt.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Defmt implementations for heapless types

use crate::{storage::Storage, vec::VecInner};
use crate::{storage::Storage, string::StringInner, vec::VecInner};
use defmt::Formatter;

impl<T, S: Storage> defmt::Format for VecInner<T, S>
Expand All @@ -12,7 +12,7 @@ where
}
}

impl<const N: usize> defmt::Format for crate::String<N>
impl<S: Storage> defmt::Format for StringInner<S>
where
u8: defmt::Format,
{
Expand Down
10 changes: 5 additions & 5 deletions src/ser.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use core::hash::{BuildHasher, Hash};

use crate::{
binary_heap::Kind as BinaryHeapKind, storage::Storage, vec::VecInner, BinaryHeap, Deque,
HistoryBuffer, IndexMap, IndexSet, LinearMap, String,
binary_heap::Kind as BinaryHeapKind, storage::Storage, string::StringInner, vec::VecInner,
BinaryHeap, Deque, HistoryBuffer, IndexMap, IndexSet, LinearMap,
};
use serde::ser::{Serialize, SerializeMap, SerializeSeq, Serializer};

Expand Down Expand Up @@ -129,10 +129,10 @@ where

// String containers

impl<const N: usize> Serialize for String<N> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
impl<S: Storage> Serialize for StringInner<S> {
fn serialize<SER>(&self, serializer: SER) -> Result<SER::Ok, SER::Error>
where
S: Serializer,
SER: Serializer,
{
serializer.serialize_str(&*self)
}
Expand Down
34 changes: 17 additions & 17 deletions src/string/drain.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
use core::{fmt, iter::FusedIterator, str::Chars};

use super::String;
use super::StringView;

/// A draining iterator for `String`.
///
/// This struct is created by the [`drain`] method on [`String`]. See its
/// This struct is created by the [`drain`] method on [`crate::String`]. See its
/// documentation for more.
///
/// [`drain`]: String::drain
pub struct Drain<'a, const N: usize> {
/// [`drain`]: crate::String::drain
pub struct Drain<'a> {
/// Will be used as &'a mut String in the destructor
pub(super) string: *mut String<N>,
/// Start of part to remove
pub(super) string: *mut StringView,
/// Stast of part to remove
pub(super) start: usize,
/// End of part to remove
pub(super) end: usize,
/// Current remaining range to remove
pub(super) iter: Chars<'a>,
}

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

unsafe impl<const N: usize> Sync for Drain<'_, N> {}
unsafe impl<const N: usize> Send for Drain<'_, N> {}
unsafe impl Sync for Drain<'_> {}
unsafe impl Send for Drain<'_> {}

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

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

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

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

impl<const N: usize> Iterator for Drain<'_, N> {
impl Iterator for Drain<'_> {
type Item = char;

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

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

impl<const N: usize> FusedIterator for Drain<'_, N> {}
impl FusedIterator for Drain<'_> {}

#[cfg(test)]
mod tests {
use super::String;
use crate::String;

#[test]
fn drain_front() {
Expand Down
Loading

0 comments on commit e43a751

Please sign in to comment.