@@ -17,6 +17,7 @@ use crate::CapacityError;
1717use crate :: LenUint ;
1818use crate :: char:: encode_utf8;
1919use crate :: utils:: MakeMaybeUninit ;
20+ use crate :: const_fn;
2021
2122#[ cfg( feature="serde" ) ]
2223use serde:: { Serialize , Deserialize , Serializer , Deserializer } ;
@@ -50,6 +51,8 @@ impl<const CAP: usize> Default for ArrayString<CAP>
5051
5152impl < const CAP : usize > ArrayString < CAP >
5253{
54+
55+ const_fn ! {
5356 /// Create a new empty `ArrayString`.
5457 ///
5558 /// Capacity is inferred from the type parameter.
@@ -67,7 +70,7 @@ impl<const CAP: usize> ArrayString<CAP>
6770 unsafe {
6871 ArrayString { xs: MaybeUninit :: uninit( ) . assume_init( ) , len: 0 }
6972 }
70- }
73+ } }
7174
7275 /// Create a new empty `ArrayString` (const fn).
7376 ///
@@ -91,6 +94,7 @@ impl<const CAP: usize> ArrayString<CAP>
9194 #[ inline]
9295 pub const fn is_empty ( & self ) -> bool { self . len ( ) == 0 }
9396
97+ const_fn ! {
9498 /// Create a new `ArrayString` from a `str`.
9599 ///
96100 /// Capacity is inferred from the type parameter.
@@ -111,8 +115,9 @@ impl<const CAP: usize> ArrayString<CAP>
111115 Ok ( ( ) ) => Ok ( arraystr) ,
112116 Err ( e) => Err ( e) ,
113117 }
114- }
118+ } }
115119
120+ const_fn ! {
116121 /// Create a new `ArrayString` from a byte string literal.
117122 ///
118123 /// **Errors** if the byte string literal is not valid UTF-8.
@@ -135,8 +140,9 @@ impl<const CAP: usize> ArrayString<CAP>
135140 vec. set_len( CAP ) ;
136141 }
137142 Ok ( vec)
138- }
143+ } }
139144
145+ const_fn ! {
140146 /// Create a new `ArrayString` value fully filled with ASCII NULL characters (`\0`). Useful
141147 /// to be used as a buffer to collect external data or as a buffer for intermediate processing.
142148 ///
@@ -157,7 +163,7 @@ impl<const CAP: usize> ArrayString<CAP>
157163 len: CAP as _
158164 }
159165 }
160- }
166+ } }
161167
162168 /// Return the capacity of the `ArrayString`.
163169 ///
@@ -214,6 +220,7 @@ impl<const CAP: usize> ArrayString<CAP>
214220 self . try_push ( c) . unwrap ( ) ;
215221 }
216222
223+ const_fn ! {
217224 /// Adds the given char to the end of the string.
218225 ///
219226 /// Returns `Ok` if the push succeeds.
@@ -245,7 +252,7 @@ impl<const CAP: usize> ArrayString<CAP>
245252 Err ( _) => Err ( CapacityError :: new( c) ) ,
246253 }
247254 }
248- }
255+ } }
249256
250257 /// Adds the given string slice to the end of the string.
251258 ///
@@ -266,6 +273,7 @@ impl<const CAP: usize> ArrayString<CAP>
266273 self . try_push_str ( s) . unwrap ( )
267274 }
268275
276+ const_fn ! {
269277 /// Adds the given string slice to the end of the string.
270278 ///
271279 /// Returns `Ok` if the push succeeds.
@@ -298,7 +306,7 @@ impl<const CAP: usize> ArrayString<CAP>
298306 self . set_len( newl) ;
299307 }
300308 Ok ( ( ) )
301- }
309+ } }
302310
303311 /// Removes the last character from the string and returns it.
304312 ///
@@ -392,13 +400,15 @@ impl<const CAP: usize> ArrayString<CAP>
392400 ch
393401 }
394402
403+ const_fn ! {
395404 /// Make the string empty.
396405 pub const fn clear( & mut self ) {
397406 unsafe {
398407 self . set_len( 0 ) ;
399408 }
400- }
409+ } }
401410
411+ const_fn ! {
402412 /// Set the strings’s length.
403413 ///
404414 /// This function is `unsafe` because it changes the notion of the
@@ -410,34 +420,37 @@ impl<const CAP: usize> ArrayString<CAP>
410420 // type invariant that capacity always fits in LenUint
411421 debug_assert!( length <= self . capacity( ) ) ;
412422 self . len = length as LenUint ;
413- }
423+ } }
414424
425+ const_fn ! {
415426 /// Return a string slice of the whole `ArrayString`.
416427 pub const fn as_str( & self ) -> & str {
417428 unsafe {
418429 let sl = slice:: from_raw_parts( self . as_ptr( ) , self . len( ) ) ;
419430 str :: from_utf8_unchecked( sl)
420431 }
421- }
432+ } }
422433
434+ const_fn ! {
423435 /// Return a mutable string slice of the whole `ArrayString`.
424436 pub const fn as_mut_str( & mut self ) -> & mut str {
425437 unsafe {
426438 let len = self . len( ) ;
427439 let sl = slice:: from_raw_parts_mut( self . as_mut_ptr( ) , len) ;
428440 str :: from_utf8_unchecked_mut( sl)
429441 }
430- }
442+ } }
431443
432444 /// Return a raw pointer to the string's buffer.
433445 pub const fn as_ptr ( & self ) -> * const u8 {
434446 self . xs . as_ptr ( ) as * const u8
435447 }
436448
449+ const_fn ! {
437450 /// Return a raw mutable pointer to the string's buffer.
438451 pub const fn as_mut_ptr( & mut self ) -> * mut u8 {
439452 self . xs. as_mut_ptr( ) as * mut u8
440- }
453+ } }
441454}
442455
443456impl < const CAP : usize > Deref for ArrayString < CAP >
0 commit comments