@@ -272,7 +272,7 @@ impl<const N: usize> String<N> {
272
272
/// ```
273
273
#[ inline]
274
274
pub fn as_str ( & self ) -> & str {
275
- unsafe { str :: from_utf8_unchecked ( self . vec . as_slice ( ) ) }
275
+ self . as_view ( ) . as_str ( )
276
276
}
277
277
278
278
/// Converts a `String` into a mutable string slice.
@@ -291,7 +291,7 @@ impl<const N: usize> String<N> {
291
291
/// ```
292
292
#[ inline]
293
293
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 ( )
295
295
}
296
296
297
297
/// Returns a mutable reference to the contents of this `String`.
@@ -353,7 +353,7 @@ impl<const N: usize> String<N> {
353
353
/// # Ok::<(), ()>(())
354
354
/// ```
355
355
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 ( )
357
357
}
358
358
359
359
/// Appends a given string slice onto the end of this `String`.
@@ -377,7 +377,7 @@ impl<const N: usize> String<N> {
377
377
#[ inline]
378
378
#[ allow( clippy:: result_unit_err) ]
379
379
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 )
381
381
}
382
382
383
383
/// Returns the maximum number of elements the String can hold.
@@ -394,7 +394,7 @@ impl<const N: usize> String<N> {
394
394
/// ```
395
395
#[ inline]
396
396
pub fn capacity ( & self ) -> usize {
397
- self . vec . capacity ( )
397
+ self . as_view ( ) . capacity ( )
398
398
}
399
399
400
400
/// Appends the given [`char`] to the end of this `String`.
@@ -420,12 +420,7 @@ impl<const N: usize> String<N> {
420
420
#[ inline]
421
421
#[ allow( clippy:: result_unit_err) ]
422
422
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)
429
424
}
430
425
431
426
/// Shortens this `String` to the specified length.
@@ -456,10 +451,7 @@ impl<const N: usize> String<N> {
456
451
/// ```
457
452
#[ inline]
458
453
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)
463
455
}
464
456
465
457
/// Removes the last character from the string buffer and returns it.
@@ -483,16 +475,7 @@ impl<const N: usize> String<N> {
483
475
/// Ok::<(), ()>(())
484
476
/// ```
485
477
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 ( )
496
479
}
497
480
498
481
/// Removes a [`char`] from this `String` at a byte position and returns it.
@@ -520,19 +503,7 @@ impl<const N: usize> String<N> {
520
503
/// ```
521
504
#[ inline]
522
505
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)
536
507
}
537
508
538
509
/// Truncates this `String`, removing all contents.
@@ -558,7 +529,7 @@ impl<const N: usize> String<N> {
558
529
/// ```
559
530
#[ inline]
560
531
pub fn clear ( & mut self ) {
561
- self . vec . clear ( )
532
+ self . as_mut_view ( ) . clear ( )
562
533
}
563
534
}
564
535
@@ -905,7 +876,7 @@ impl<const N: usize> Clone for String<N> {
905
876
906
877
impl < const N : usize > fmt:: Debug for String < N > {
907
878
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
908
- < str as fmt:: Debug > :: fmt ( self , f)
879
+ self . as_view ( ) . fmt ( f)
909
880
}
910
881
}
911
882
@@ -917,7 +888,7 @@ impl fmt::Debug for StringView {
917
888
918
889
impl < const N : usize > fmt:: Display for String < N > {
919
890
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
920
- < str as fmt:: Display > :: fmt ( self , f)
891
+ self . as_view ( ) . fmt ( f)
921
892
}
922
893
}
923
894
@@ -930,7 +901,7 @@ impl fmt::Display for StringView {
930
901
impl < const N : usize > hash:: Hash for String < N > {
931
902
#[ inline]
932
903
fn hash < H : hash:: Hasher > ( & self , hasher : & mut H ) {
933
- < str as hash:: Hash > :: hash ( self , hasher)
904
+ self . as_view ( ) . hash ( hasher)
934
905
}
935
906
}
936
907
@@ -943,11 +914,11 @@ impl hash::Hash for StringView {
943
914
944
915
impl < const N : usize > fmt:: Write for String < N > {
945
916
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 )
947
918
}
948
919
949
920
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 )
951
922
}
952
923
}
953
924
@@ -965,7 +936,7 @@ impl<const N: usize> ops::Deref for String<N> {
965
936
type Target = str ;
966
937
967
938
fn deref ( & self ) -> & str {
968
- self . as_str ( )
939
+ self . as_view ( ) . deref ( )
969
940
}
970
941
}
971
942
@@ -979,7 +950,7 @@ impl ops::Deref for StringView {
979
950
980
951
impl < const N : usize > ops:: DerefMut for String < N > {
981
952
fn deref_mut ( & mut self ) -> & mut str {
982
- self . as_mut_str ( )
953
+ self . as_mut_view ( ) . deref_mut ( )
983
954
}
984
955
}
985
956
@@ -1006,7 +977,7 @@ impl AsRef<str> for StringView {
1006
977
impl < const N : usize > AsRef < [ u8 ] > for String < N > {
1007
978
#[ inline]
1008
979
fn as_ref ( & self ) -> & [ u8 ] {
1009
- self . as_bytes ( )
980
+ self . as_view ( ) . as_bytes ( )
1010
981
}
1011
982
}
1012
983
0 commit comments