@@ -2,7 +2,6 @@ use std::borrow::Borrow;
2
2
use std:: cmp;
3
3
use std:: fmt;
4
4
use std:: hash:: { Hash , Hasher } ;
5
- use std:: mem;
6
5
use std:: ptr;
7
6
use std:: ops:: { Deref , DerefMut } ;
8
7
use std:: str;
@@ -17,6 +16,8 @@ use char::encode_utf8;
17
16
#[ cfg( feature="serde-1" ) ]
18
17
use serde:: { Serialize , Deserialize , Serializer , Deserializer } ;
19
18
19
+ use super :: MaybeUninitCopy ;
20
+
20
21
/// A string with a fixed capacity.
21
22
///
22
23
/// The `ArrayString` is a string backed by a fixed size array. It keeps track
@@ -25,20 +26,25 @@ use serde::{Serialize, Deserialize, Serializer, Deserializer};
25
26
/// The string is a contiguous value that you can store directly on the stack
26
27
/// if needed.
27
28
#[ derive( Copy ) ]
28
- pub struct ArrayString < A : Array < Item =u8 > > {
29
- // FIXME: Use Copyable union for xs when we can
30
- xs : A ,
29
+ pub struct ArrayString < A >
30
+ where A : Array < Item =u8 > + Copy
31
+ {
32
+ xs : MaybeUninitCopy < A > ,
31
33
len : A :: Index ,
32
34
}
33
35
34
- impl < A : Array < Item =u8 > > Default for ArrayString < A > {
36
+ impl < A > Default for ArrayString < A >
37
+ where A : Array < Item =u8 > + Copy
38
+ {
35
39
/// Return an empty `ArrayString`
36
40
fn default ( ) -> ArrayString < A > {
37
41
ArrayString :: new ( )
38
42
}
39
43
}
40
44
41
- impl < A : Array < Item =u8 > > ArrayString < A > {
45
+ impl < A > ArrayString < A >
46
+ where A : Array < Item =u8 > + Copy
47
+ {
42
48
/// Create a new empty `ArrayString`.
43
49
///
44
50
/// Capacity is inferred from the type parameter.
@@ -54,8 +60,7 @@ impl<A: Array<Item=u8>> ArrayString<A> {
54
60
pub fn new ( ) -> ArrayString < A > {
55
61
unsafe {
56
62
ArrayString {
57
- // FIXME: Use Copyable union for xs when we can
58
- xs : mem:: zeroed ( ) ,
63
+ xs : MaybeUninitCopy :: uninitialized ( ) ,
59
64
len : Index :: from ( 0 ) ,
60
65
}
61
66
}
@@ -91,11 +96,12 @@ impl<A: Array<Item=u8>> ArrayString<A> {
91
96
/// let string = ArrayString::from_byte_string(b"hello world").unwrap();
92
97
/// ```
93
98
pub fn from_byte_string ( b : & A ) -> Result < Self , Utf8Error > {
94
- let mut arraystr = Self :: new ( ) ;
95
- let s = try!( str:: from_utf8 ( b. as_slice ( ) ) ) ;
96
- let _result = arraystr. try_push_str ( s) ;
97
- debug_assert ! ( _result. is_ok( ) ) ;
98
- Ok ( arraystr)
99
+ let len = str:: from_utf8 ( b. as_slice ( ) ) ?. len ( ) ;
100
+ debug_assert_eq ! ( len, A :: capacity( ) ) ;
101
+ Ok ( ArrayString {
102
+ xs : MaybeUninitCopy :: from ( * b) ,
103
+ len : Index :: from ( A :: capacity ( ) ) ,
104
+ } )
99
105
}
100
106
101
107
/// Return the capacity of the `ArrayString`.
@@ -213,7 +219,7 @@ impl<A: Array<Item=u8>> ArrayString<A> {
213
219
return Err ( CapacityError :: new ( s) ) ;
214
220
}
215
221
unsafe {
216
- let dst = self . xs . as_mut_ptr ( ) . offset ( self . len ( ) as isize ) ;
222
+ let dst = self . xs . ptr_mut ( ) . offset ( self . len ( ) as isize ) ;
217
223
let src = s. as_ptr ( ) ;
218
224
ptr:: copy_nonoverlapping ( src, dst, s. len ( ) ) ;
219
225
let newl = self . len ( ) + s. len ( ) ;
@@ -307,8 +313,8 @@ impl<A: Array<Item=u8>> ArrayString<A> {
307
313
let next = idx + ch. len_utf8 ( ) ;
308
314
let len = self . len ( ) ;
309
315
unsafe {
310
- ptr:: copy ( self . xs . as_ptr ( ) . offset ( next as isize ) ,
311
- self . xs . as_mut_ptr ( ) . offset ( idx as isize ) ,
316
+ ptr:: copy ( self . xs . ptr ( ) . offset ( next as isize ) ,
317
+ self . xs . ptr_mut ( ) . offset ( idx as isize ) ,
312
318
len - next) ;
313
319
self . set_len ( len - ( next - idx) ) ;
314
320
}
@@ -342,75 +348,99 @@ impl<A: Array<Item=u8>> ArrayString<A> {
342
348
343
349
/// Return a mutable slice of the whole string’s buffer
344
350
unsafe fn raw_mut_bytes ( & mut self ) -> & mut [ u8 ] {
345
- slice:: from_raw_parts_mut ( self . xs . as_mut_ptr ( ) , self . capacity ( ) )
351
+ slice:: from_raw_parts_mut ( self . xs . ptr_mut ( ) , self . capacity ( ) )
346
352
}
347
353
}
348
354
349
- impl < A : Array < Item =u8 > > Deref for ArrayString < A > {
355
+ impl < A > Deref for ArrayString < A >
356
+ where A : Array < Item =u8 > + Copy
357
+ {
350
358
type Target = str ;
351
359
#[ inline]
352
360
fn deref ( & self ) -> & str {
353
361
unsafe {
354
- let sl = slice:: from_raw_parts ( self . xs . as_ptr ( ) , self . len . to_usize ( ) ) ;
362
+ let sl = slice:: from_raw_parts ( self . xs . ptr ( ) , self . len . to_usize ( ) ) ;
355
363
str:: from_utf8_unchecked ( sl)
356
364
}
357
365
}
358
366
}
359
367
360
- impl < A : Array < Item =u8 > > DerefMut for ArrayString < A > {
368
+ impl < A > DerefMut for ArrayString < A >
369
+ where A : Array < Item =u8 > + Copy
370
+ {
361
371
#[ inline]
362
372
fn deref_mut ( & mut self ) -> & mut str {
363
373
unsafe {
364
- let sl = slice:: from_raw_parts_mut ( self . xs . as_mut_ptr ( ) , self . len . to_usize ( ) ) ;
374
+ let sl = slice:: from_raw_parts_mut ( self . xs . ptr_mut ( ) , self . len . to_usize ( ) ) ;
365
375
str:: from_utf8_unchecked_mut ( sl)
366
376
}
367
377
}
368
378
}
369
379
370
- impl < A : Array < Item =u8 > > PartialEq for ArrayString < A > {
380
+ impl < A > PartialEq for ArrayString < A >
381
+ where A : Array < Item =u8 > + Copy
382
+ {
371
383
fn eq ( & self , rhs : & Self ) -> bool {
372
384
* * self == * * rhs
373
385
}
374
386
}
375
387
376
- impl < A : Array < Item =u8 > > PartialEq < str > for ArrayString < A > {
388
+ impl < A > PartialEq < str > for ArrayString < A >
389
+ where A : Array < Item =u8 > + Copy
390
+ {
377
391
fn eq ( & self , rhs : & str ) -> bool {
378
392
& * * self == rhs
379
393
}
380
394
}
381
395
382
- impl < A : Array < Item =u8 > > PartialEq < ArrayString < A > > for str {
396
+ impl < A > PartialEq < ArrayString < A > > for str
397
+ where A : Array < Item =u8 > + Copy
398
+ {
383
399
fn eq ( & self , rhs : & ArrayString < A > ) -> bool {
384
400
self == & * * rhs
385
401
}
386
402
}
387
403
388
- impl < A : Array < Item =u8 > > Eq for ArrayString < A > { }
404
+ impl < A > Eq for ArrayString < A >
405
+ where A : Array < Item =u8 > + Copy
406
+ { }
389
407
390
- impl < A : Array < Item =u8 > > Hash for ArrayString < A > {
408
+ impl < A > Hash for ArrayString < A >
409
+ where A : Array < Item =u8 > + Copy
410
+ {
391
411
fn hash < H : Hasher > ( & self , h : & mut H ) {
392
412
( * * self ) . hash ( h)
393
413
}
394
414
}
395
415
396
- impl < A : Array < Item =u8 > > Borrow < str > for ArrayString < A > {
416
+ impl < A > Borrow < str > for ArrayString < A >
417
+ where A : Array < Item =u8 > + Copy
418
+ {
397
419
fn borrow ( & self ) -> & str { self }
398
420
}
399
421
400
- impl < A : Array < Item =u8 > > AsRef < str > for ArrayString < A > {
422
+ impl < A > AsRef < str > for ArrayString < A >
423
+ where A : Array < Item =u8 > + Copy
424
+ {
401
425
fn as_ref ( & self ) -> & str { self }
402
426
}
403
427
404
- impl < A : Array < Item =u8 > > fmt:: Debug for ArrayString < A > {
428
+ impl < A > fmt:: Debug for ArrayString < A >
429
+ where A : Array < Item =u8 > + Copy
430
+ {
405
431
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result { ( * * self ) . fmt ( f) }
406
432
}
407
433
408
- impl < A : Array < Item =u8 > > fmt:: Display for ArrayString < A > {
434
+ impl < A > fmt:: Display for ArrayString < A >
435
+ where A : Array < Item =u8 > + Copy
436
+ {
409
437
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result { ( * * self ) . fmt ( f) }
410
438
}
411
439
412
440
/// `Write` appends written data to the end of the string.
413
- impl < A : Array < Item =u8 > > fmt:: Write for ArrayString < A > {
441
+ impl < A > fmt:: Write for ArrayString < A >
442
+ where A : Array < Item =u8 > + Copy
443
+ {
414
444
fn write_char ( & mut self , c : char ) -> fmt:: Result {
415
445
self . try_push ( c) . map_err ( |_| fmt:: Error )
416
446
}
@@ -420,7 +450,9 @@ impl<A: Array<Item=u8>> fmt::Write for ArrayString<A> {
420
450
}
421
451
}
422
452
423
- impl < A : Array < Item =u8 > + Copy > Clone for ArrayString < A > {
453
+ impl < A > Clone for ArrayString < A >
454
+ where A : Array < Item =u8 > + Copy
455
+ {
424
456
fn clone ( & self ) -> ArrayString < A > {
425
457
* self
426
458
}
@@ -431,7 +463,9 @@ impl<A: Array<Item=u8> + Copy> Clone for ArrayString<A> {
431
463
}
432
464
}
433
465
434
- impl < A : Array < Item =u8 > > PartialOrd for ArrayString < A > {
466
+ impl < A > PartialOrd for ArrayString < A >
467
+ where A : Array < Item =u8 > + Copy
468
+ {
435
469
fn partial_cmp ( & self , rhs : & Self ) -> Option < cmp:: Ordering > {
436
470
( * * self ) . partial_cmp ( & * * rhs)
437
471
}
@@ -441,7 +475,9 @@ impl<A: Array<Item=u8>> PartialOrd for ArrayString<A> {
441
475
fn ge ( & self , rhs : & Self ) -> bool { * * self >= * * rhs }
442
476
}
443
477
444
- impl < A : Array < Item =u8 > > PartialOrd < str > for ArrayString < A > {
478
+ impl < A > PartialOrd < str > for ArrayString < A >
479
+ where A : Array < Item =u8 > + Copy
480
+ {
445
481
fn partial_cmp ( & self , rhs : & str ) -> Option < cmp:: Ordering > {
446
482
( * * self ) . partial_cmp ( rhs)
447
483
}
@@ -451,7 +487,9 @@ impl<A: Array<Item=u8>> PartialOrd<str> for ArrayString<A> {
451
487
fn ge ( & self , rhs : & str ) -> bool { & * * self >= rhs }
452
488
}
453
489
454
- impl < A : Array < Item =u8 > > PartialOrd < ArrayString < A > > for str {
490
+ impl < A > PartialOrd < ArrayString < A > > for str
491
+ where A : Array < Item =u8 > + Copy
492
+ {
455
493
fn partial_cmp ( & self , rhs : & ArrayString < A > ) -> Option < cmp:: Ordering > {
456
494
self . partial_cmp ( & * * rhs)
457
495
}
@@ -461,15 +499,19 @@ impl<A: Array<Item=u8>> PartialOrd<ArrayString<A>> for str {
461
499
fn ge ( & self , rhs : & ArrayString < A > ) -> bool { self >= & * * rhs }
462
500
}
463
501
464
- impl < A : Array < Item =u8 > > Ord for ArrayString < A > {
502
+ impl < A > Ord for ArrayString < A >
503
+ where A : Array < Item =u8 > + Copy
504
+ {
465
505
fn cmp ( & self , rhs : & Self ) -> cmp:: Ordering {
466
506
( * * self ) . cmp ( & * * rhs)
467
507
}
468
508
}
469
509
470
510
#[ cfg( feature="serde-1" ) ]
471
511
/// Requires crate feature `"serde-1"`
472
- impl < A : Array < Item =u8 > > Serialize for ArrayString < A > {
512
+ impl < A > Serialize for ArrayString < A >
513
+ where A : Array < Item =u8 > + Copy
514
+ {
473
515
fn serialize < S > ( & self , serializer : S ) -> Result < S :: Ok , S :: Error >
474
516
where S : Serializer
475
517
{
@@ -479,7 +521,9 @@ impl<A: Array<Item=u8>> Serialize for ArrayString<A> {
479
521
480
522
#[ cfg( feature="serde-1" ) ]
481
523
/// Requires crate feature `"serde-1"`
482
- impl < ' de , A : Array < Item =u8 > > Deserialize < ' de > for ArrayString < A > {
524
+ impl < ' de , A > Deserialize < ' de > for ArrayString < A >
525
+ where A : Array < Item =u8 > + Copy
526
+ {
483
527
fn deserialize < D > ( deserializer : D ) -> Result < Self , D :: Error >
484
528
where D : Deserializer < ' de >
485
529
{
@@ -488,7 +532,7 @@ impl<'de, A: Array<Item=u8>> Deserialize<'de> for ArrayString<A> {
488
532
489
533
struct ArrayStringVisitor < A : Array < Item =u8 > > ( PhantomData < A > ) ;
490
534
491
- impl < ' de , A : Array < Item =u8 > > Visitor < ' de > for ArrayStringVisitor < A > {
535
+ impl < ' de , A : Copy + Array < Item =u8 > > Visitor < ' de > for ArrayStringVisitor < A > {
492
536
type Value = ArrayString < A > ;
493
537
494
538
fn expecting ( & self , formatter : & mut fmt:: Formatter ) -> fmt:: Result {
0 commit comments