Skip to content

Commit 479e5ef

Browse files
String: delegate implementations to StringView
1 parent c21425b commit 479e5ef

File tree

1 file changed

+18
-47
lines changed

1 file changed

+18
-47
lines changed

src/string.rs

Lines changed: 18 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ impl<const N: usize> String<N> {
272272
/// ```
273273
#[inline]
274274
pub fn as_str(&self) -> &str {
275-
unsafe { str::from_utf8_unchecked(self.vec.as_slice()) }
275+
self.as_view().as_str()
276276
}
277277

278278
/// Converts a `String` into a mutable string slice.
@@ -291,7 +291,7 @@ impl<const N: usize> String<N> {
291291
/// ```
292292
#[inline]
293293
pub fn as_mut_str(&mut self) -> &mut str {
294-
unsafe { str::from_utf8_unchecked_mut(self.vec.as_mut_slice()) }
294+
self.as_mut_view().as_mut_str()
295295
}
296296

297297
/// Returns a mutable reference to the contents of this `String`.
@@ -353,7 +353,7 @@ impl<const N: usize> String<N> {
353353
/// # Ok::<(), ()>(())
354354
/// ```
355355
pub unsafe fn as_mut_vec_view(&mut self) -> &mut VecView<u8> {
356-
&mut self.vec
356+
self.as_mut_view().as_mut_vec_view()
357357
}
358358

359359
/// Appends a given string slice onto the end of this `String`.
@@ -377,7 +377,7 @@ impl<const N: usize> String<N> {
377377
#[inline]
378378
#[allow(clippy::result_unit_err)]
379379
pub fn push_str(&mut self, string: &str) -> Result<(), ()> {
380-
self.vec.extend_from_slice(string.as_bytes())
380+
self.as_mut_view().push_str(string)
381381
}
382382

383383
/// Returns the maximum number of elements the String can hold.
@@ -394,7 +394,7 @@ impl<const N: usize> String<N> {
394394
/// ```
395395
#[inline]
396396
pub fn capacity(&self) -> usize {
397-
self.vec.capacity()
397+
self.as_view().capacity()
398398
}
399399

400400
/// Appends the given [`char`] to the end of this `String`.
@@ -420,12 +420,7 @@ impl<const N: usize> String<N> {
420420
#[inline]
421421
#[allow(clippy::result_unit_err)]
422422
pub fn push(&mut self, c: char) -> Result<(), ()> {
423-
match c.len_utf8() {
424-
1 => self.vec.push(c as u8).map_err(|_| {}),
425-
_ => self
426-
.vec
427-
.extend_from_slice(c.encode_utf8(&mut [0; 4]).as_bytes()),
428-
}
423+
self.as_mut_view().push(c)
429424
}
430425

431426
/// Shortens this `String` to the specified length.
@@ -456,10 +451,7 @@ impl<const N: usize> String<N> {
456451
/// ```
457452
#[inline]
458453
pub fn truncate(&mut self, new_len: usize) {
459-
if new_len <= self.len() {
460-
assert!(self.is_char_boundary(new_len));
461-
self.vec.truncate(new_len)
462-
}
454+
self.as_mut_view().truncate(new_len)
463455
}
464456

465457
/// Removes the last character from the string buffer and returns it.
@@ -483,16 +475,7 @@ impl<const N: usize> String<N> {
483475
/// Ok::<(), ()>(())
484476
/// ```
485477
pub fn pop(&mut self) -> Option<char> {
486-
let ch = self.chars().next_back()?;
487-
488-
// pop bytes that correspond to `ch`
489-
for _ in 0..ch.len_utf8() {
490-
unsafe {
491-
self.vec.pop_unchecked();
492-
}
493-
}
494-
495-
Some(ch)
478+
self.as_mut_view().pop()
496479
}
497480

498481
/// Removes a [`char`] from this `String` at a byte position and returns it.
@@ -520,19 +503,7 @@ impl<const N: usize> String<N> {
520503
/// ```
521504
#[inline]
522505
pub fn remove(&mut self, index: usize) -> char {
523-
let ch = match self[index..].chars().next() {
524-
Some(ch) => ch,
525-
None => panic!("cannot remove a char from the end of a string"),
526-
};
527-
528-
let next = index + ch.len_utf8();
529-
let len = self.len();
530-
let ptr = self.vec.as_mut_ptr();
531-
unsafe {
532-
core::ptr::copy(ptr.add(next), ptr.add(index), len - next);
533-
self.vec.set_len(len - (next - index));
534-
}
535-
ch
506+
self.as_mut_view().remove(index)
536507
}
537508

538509
/// Truncates this `String`, removing all contents.
@@ -558,7 +529,7 @@ impl<const N: usize> String<N> {
558529
/// ```
559530
#[inline]
560531
pub fn clear(&mut self) {
561-
self.vec.clear()
532+
self.as_mut_view().clear()
562533
}
563534
}
564535

@@ -905,7 +876,7 @@ impl<const N: usize> Clone for String<N> {
905876

906877
impl<const N: usize> fmt::Debug for String<N> {
907878
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
908-
<str as fmt::Debug>::fmt(self, f)
879+
self.as_view().fmt(f)
909880
}
910881
}
911882

@@ -917,7 +888,7 @@ impl fmt::Debug for StringView {
917888

918889
impl<const N: usize> fmt::Display for String<N> {
919890
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
920-
<str as fmt::Display>::fmt(self, f)
891+
self.as_view().fmt(f)
921892
}
922893
}
923894

@@ -930,7 +901,7 @@ impl fmt::Display for StringView {
930901
impl<const N: usize> hash::Hash for String<N> {
931902
#[inline]
932903
fn hash<H: hash::Hasher>(&self, hasher: &mut H) {
933-
<str as hash::Hash>::hash(self, hasher)
904+
self.as_view().hash(hasher)
934905
}
935906
}
936907

@@ -943,11 +914,11 @@ impl hash::Hash for StringView {
943914

944915
impl<const N: usize> fmt::Write for String<N> {
945916
fn write_str(&mut self, s: &str) -> Result<(), fmt::Error> {
946-
self.push_str(s).map_err(|_| fmt::Error)
917+
self.as_mut_view().write_str(s)
947918
}
948919

949920
fn write_char(&mut self, c: char) -> Result<(), fmt::Error> {
950-
self.push(c).map_err(|_| fmt::Error)
921+
self.as_mut_view().write_char(c)
951922
}
952923
}
953924

@@ -965,7 +936,7 @@ impl<const N: usize> ops::Deref for String<N> {
965936
type Target = str;
966937

967938
fn deref(&self) -> &str {
968-
self.as_str()
939+
self.as_view().deref()
969940
}
970941
}
971942

@@ -979,7 +950,7 @@ impl ops::Deref for StringView {
979950

980951
impl<const N: usize> ops::DerefMut for String<N> {
981952
fn deref_mut(&mut self) -> &mut str {
982-
self.as_mut_str()
953+
self.as_mut_view().deref_mut()
983954
}
984955
}
985956

@@ -1006,7 +977,7 @@ impl AsRef<str> for StringView {
1006977
impl<const N: usize> AsRef<[u8]> for String<N> {
1007978
#[inline]
1008979
fn as_ref(&self) -> &[u8] {
1009-
self.as_bytes()
980+
self.as_view().as_bytes()
1010981
}
1011982
}
1012983

0 commit comments

Comments
 (0)