@@ -221,6 +221,30 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type {
221
221
mem .copy (T , self .items [old_len .. ], items );
222
222
}
223
223
224
+ /// Append an unaligned slice of items to the list. Allocates more
225
+ /// memory as necessary. Only call this function if calling
226
+ /// `appendSlice` instead would be a compile error.
227
+ pub fn appendUnalignedSlice (self : * Self , items : []align (1 ) const T ) Allocator .Error ! void {
228
+ try self .ensureUnusedCapacity (items .len );
229
+ self .appendUnalignedSliceAssumeCapacity (items );
230
+ }
231
+
232
+ /// Append the slice of items to the list, asserting the capacity is already
233
+ /// enough to store the new items. **Does not** invalidate pointers.
234
+ /// Only call this function if calling `appendSliceAssumeCapacity` instead
235
+ /// would be a compile error.
236
+ pub fn appendUnalignedSliceAssumeCapacity (self : * Self , items : []align (1 ) const T ) void {
237
+ const old_len = self .items .len ;
238
+ const new_len = old_len + items .len ;
239
+ assert (new_len <= self .capacity );
240
+ self .items .len = new_len ;
241
+ @memcpy (
242
+ @ptrCast ([* ]align (@alignOf (T )) u8 , self .items .ptr + old_len ),
243
+ @ptrCast ([* ]const u8 , items .ptr ),
244
+ items .len * @sizeOf (T ),
245
+ );
246
+ }
247
+
224
248
pub const Writer = if (T != u8 )
225
249
@compileError ("The Writer interface is only defined for ArrayList(u8) " ++
226
250
"but the given type is ArrayList(" ++ @typeName (T ) ++ ")" )
@@ -592,6 +616,29 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ
592
616
mem .copy (T , self .items [old_len .. ], items );
593
617
}
594
618
619
+ /// Append the slice of items to the list. Allocates more
620
+ /// memory as necessary. Only call this function if a call to `appendSlice` instead would
621
+ /// be a compile error.
622
+ pub fn appendUnalignedSlice (self : * Self , allocator : Allocator , items : []align (1 ) const T ) Allocator .Error ! void {
623
+ try self .ensureUnusedCapacity (allocator , items .len );
624
+ self .appendUnalignedSliceAssumeCapacity (items );
625
+ }
626
+
627
+ /// Append an unaligned slice of items to the list, asserting the capacity is enough
628
+ /// to store the new items. Only call this function if a call to `appendSliceAssumeCapacity`
629
+ /// instead would be a compile error.
630
+ pub fn appendUnalignedSliceAssumeCapacity (self : * Self , items : []align (1 ) const T ) void {
631
+ const old_len = self .items .len ;
632
+ const new_len = old_len + items .len ;
633
+ assert (new_len <= self .capacity );
634
+ self .items .len = new_len ;
635
+ @memcpy (
636
+ @ptrCast ([* ]align (@alignOf (T )) u8 , self .items .ptr + old_len ),
637
+ @ptrCast ([* ]const u8 , items .ptr ),
638
+ items .len * @sizeOf (T ),
639
+ );
640
+ }
641
+
595
642
pub const WriterContext = struct {
596
643
self : * Self ,
597
644
allocator : Allocator ,
@@ -899,6 +946,14 @@ test "std.ArrayList/ArrayListUnmanaged.basic" {
899
946
try testing .expect (list .pop () == 1 );
900
947
try testing .expect (list .items .len == 9 );
901
948
949
+ var unaligned : [3 ]i32 align (1 ) = [_ ]i32 { 4 , 5 , 6 };
950
+ list .appendUnalignedSlice (& unaligned ) catch unreachable ;
951
+ try testing .expect (list .items .len == 12 );
952
+ try testing .expect (list .pop () == 6 );
953
+ try testing .expect (list .pop () == 5 );
954
+ try testing .expect (list .pop () == 4 );
955
+ try testing .expect (list .items .len == 9 );
956
+
902
957
list .appendSlice (&[_ ]i32 {}) catch unreachable ;
903
958
try testing .expect (list .items .len == 9 );
904
959
@@ -941,6 +996,14 @@ test "std.ArrayList/ArrayListUnmanaged.basic" {
941
996
try testing .expect (list .pop () == 1 );
942
997
try testing .expect (list .items .len == 9 );
943
998
999
+ var unaligned : [3 ]i32 align (1 ) = [_ ]i32 { 4 , 5 , 6 };
1000
+ list .appendUnalignedSlice (a , & unaligned ) catch unreachable ;
1001
+ try testing .expect (list .items .len == 12 );
1002
+ try testing .expect (list .pop () == 6 );
1003
+ try testing .expect (list .pop () == 5 );
1004
+ try testing .expect (list .pop () == 4 );
1005
+ try testing .expect (list .items .len == 9 );
1006
+
944
1007
list .appendSlice (a , &[_ ]i32 {}) catch unreachable ;
945
1008
try testing .expect (list .items .len == 9 );
946
1009
0 commit comments