@@ -62,7 +62,7 @@ impl<const CAP: usize> ArrayString<CAP>
62
62
/// assert_eq!(&string[..], "foo");
63
63
/// assert_eq!(string.capacity(), 16);
64
64
/// ```
65
- pub fn new ( ) -> ArrayString < CAP > {
65
+ pub const fn new ( ) -> ArrayString < CAP > {
66
66
assert_capacity_limit ! ( CAP ) ;
67
67
unsafe {
68
68
ArrayString { xs : MaybeUninit :: uninit ( ) . assume_init ( ) , len : 0 }
@@ -105,10 +105,12 @@ impl<const CAP: usize> ArrayString<CAP>
105
105
/// assert_eq!(string.len(), 3);
106
106
/// assert_eq!(string.capacity(), 3);
107
107
/// ```
108
- pub fn from ( s : & str ) -> Result < Self , CapacityError < & str > > {
108
+ pub const fn from ( s : & str ) -> Result < Self , CapacityError < & str > > {
109
109
let mut arraystr = Self :: new ( ) ;
110
- arraystr. try_push_str ( s) ?;
111
- Ok ( arraystr)
110
+ match arraystr. try_push_str ( s) {
111
+ Ok ( ( ) ) => Ok ( arraystr) ,
112
+ Err ( e) => Err ( e) ,
113
+ }
112
114
}
113
115
114
116
/// Create a new `ArrayString` from a byte string literal.
@@ -120,9 +122,12 @@ impl<const CAP: usize> ArrayString<CAP>
120
122
///
121
123
/// let string = ArrayString::from_byte_string(b"hello world").unwrap();
122
124
/// ```
123
- pub fn from_byte_string ( b : & [ u8 ; CAP ] ) -> Result < Self , Utf8Error > {
124
- let len = str:: from_utf8 ( b) ?. len ( ) ;
125
- debug_assert_eq ! ( len, CAP ) ;
125
+ pub const fn from_byte_string ( b : & [ u8 ; CAP ] ) -> Result < Self , Utf8Error > {
126
+ let len = match str:: from_utf8 ( b) {
127
+ Ok ( str) => str. len ( ) ,
128
+ Err ( e) => return Err ( e) ,
129
+ } ;
130
+ debug_assert ! ( len == CAP ) ;
126
131
let mut vec = Self :: new ( ) ;
127
132
unsafe {
128
133
( b as * const [ u8 ; CAP ] as * const [ MaybeUninit < u8 > ; CAP ] )
@@ -142,7 +147,7 @@ impl<const CAP: usize> ArrayString<CAP>
142
147
/// assert_eq!(string.len(), 16);
143
148
/// ```
144
149
#[ inline]
145
- pub fn zero_filled ( ) -> Self {
150
+ pub const fn zero_filled ( ) -> Self {
146
151
assert_capacity_limit ! ( CAP ) ;
147
152
// SAFETY: `assert_capacity_limit` asserts that `len` won't overflow and
148
153
// `zeroed` fully fills the array with nulls.
@@ -227,7 +232,7 @@ impl<const CAP: usize> ArrayString<CAP>
227
232
/// assert_eq!(&string[..], "ab");
228
233
/// assert_eq!(overflow.unwrap_err().element(), 'c');
229
234
/// ```
230
- pub fn try_push ( & mut self , c : char ) -> Result < ( ) , CapacityError < char > > {
235
+ pub const fn try_push ( & mut self , c : char ) -> Result < ( ) , CapacityError < char > > {
231
236
let len = self . len ( ) ;
232
237
unsafe {
233
238
let ptr = self . as_mut_ptr ( ) . add ( len) ;
@@ -281,7 +286,7 @@ impl<const CAP: usize> ArrayString<CAP>
281
286
/// assert_eq!(overflow1.unwrap_err().element(), "bc");
282
287
/// assert_eq!(overflow2.unwrap_err().element(), "ef");
283
288
/// ```
284
- pub fn try_push_str < ' a > ( & mut self , s : & ' a str ) -> Result < ( ) , CapacityError < & ' a str > > {
289
+ pub const fn try_push_str < ' a > ( & mut self , s : & ' a str ) -> Result < ( ) , CapacityError < & ' a str > > {
285
290
if s. len ( ) > self . capacity ( ) - self . len ( ) {
286
291
return Err ( CapacityError :: new ( s) ) ;
287
292
}
@@ -340,7 +345,7 @@ impl<const CAP: usize> ArrayString<CAP>
340
345
/// ```
341
346
pub fn truncate ( & mut self , new_len : usize ) {
342
347
if new_len <= self . len ( ) {
343
- assert ! ( self . is_char_boundary( new_len) ) ;
348
+ assert ! ( self . as_str ( ) . is_char_boundary( new_len) ) ;
344
349
unsafe {
345
350
// In libstd truncate is called on the underlying vector,
346
351
// which in turns drops each element.
@@ -388,7 +393,7 @@ impl<const CAP: usize> ArrayString<CAP>
388
393
}
389
394
390
395
/// Make the string empty.
391
- pub fn clear ( & mut self ) {
396
+ pub const fn clear ( & mut self ) {
392
397
unsafe {
393
398
self . set_len ( 0 ) ;
394
399
}
@@ -401,29 +406,36 @@ impl<const CAP: usize> ArrayString<CAP>
401
406
///
402
407
/// This method uses *debug assertions* to check the validity of `length`
403
408
/// and may use other debug assertions.
404
- pub unsafe fn set_len ( & mut self , length : usize ) {
409
+ pub const unsafe fn set_len ( & mut self , length : usize ) {
405
410
// type invariant that capacity always fits in LenUint
406
411
debug_assert ! ( length <= self . capacity( ) ) ;
407
412
self . len = length as LenUint ;
408
413
}
409
414
410
415
/// Return a string slice of the whole `ArrayString`.
411
- pub fn as_str ( & self ) -> & str {
412
- self
416
+ pub const fn as_str ( & self ) -> & str {
417
+ unsafe {
418
+ let sl = slice:: from_raw_parts ( self . as_ptr ( ) , self . len ( ) ) ;
419
+ str:: from_utf8_unchecked ( sl)
420
+ }
413
421
}
414
422
415
423
/// Return a mutable string slice of the whole `ArrayString`.
416
- pub fn as_mut_str ( & mut self ) -> & mut str {
417
- self
424
+ pub const fn as_mut_str ( & mut self ) -> & mut str {
425
+ unsafe {
426
+ let len = self . len ( ) ;
427
+ let sl = slice:: from_raw_parts_mut ( self . as_mut_ptr ( ) , len) ;
428
+ str:: from_utf8_unchecked_mut ( sl)
429
+ }
418
430
}
419
431
420
432
/// Return a raw pointer to the string's buffer.
421
- pub fn as_ptr ( & self ) -> * const u8 {
433
+ pub const fn as_ptr ( & self ) -> * const u8 {
422
434
self . xs . as_ptr ( ) as * const u8
423
435
}
424
436
425
437
/// Return a raw mutable pointer to the string's buffer.
426
- pub fn as_mut_ptr ( & mut self ) -> * mut u8 {
438
+ pub const fn as_mut_ptr ( & mut self ) -> * mut u8 {
427
439
self . xs . as_mut_ptr ( ) as * mut u8
428
440
}
429
441
}
@@ -433,22 +445,15 @@ impl<const CAP: usize> Deref for ArrayString<CAP>
433
445
type Target = str ;
434
446
#[ inline]
435
447
fn deref ( & self ) -> & str {
436
- unsafe {
437
- let sl = slice:: from_raw_parts ( self . as_ptr ( ) , self . len ( ) ) ;
438
- str:: from_utf8_unchecked ( sl)
439
- }
448
+ self . as_str ( )
440
449
}
441
450
}
442
451
443
452
impl < const CAP : usize > DerefMut for ArrayString < CAP >
444
453
{
445
454
#[ inline]
446
455
fn deref_mut ( & mut self ) -> & mut str {
447
- unsafe {
448
- let len = self . len ( ) ;
449
- let sl = slice:: from_raw_parts_mut ( self . as_mut_ptr ( ) , len) ;
450
- str:: from_utf8_unchecked_mut ( sl)
451
- }
456
+ self . as_mut_str ( )
452
457
}
453
458
}
454
459
0 commit comments