@@ -2541,13 +2541,17 @@ pub const Allocating = struct {
25412541 alignment : std.mem.Alignment ,
25422542
25432543 pub fn init (allocator : Allocator ) Allocating {
2544+ return .initAligned (allocator , .of (u8 ));
2545+ }
2546+
2547+ pub fn initAligned (allocator : Allocator , alignment : std.mem.Alignment ) Allocating {
25442548 return .{
25452549 .allocator = allocator ,
25462550 .writer = .{
25472551 .buffer = &.{},
25482552 .vtable = & vtable ,
25492553 },
2550- .alignment = . of ( u8 ) ,
2554+ .alignment = alignment ,
25512555 };
25522556 }
25532557
@@ -2775,16 +2779,36 @@ pub const Allocating = struct {
27752779 a .ensureUnusedCapacity (minimum_len ) catch return error .WriteFailed ;
27762780 }
27772781
2778- test Allocating {
2779- var a : Allocating = .init (testing .allocator );
2782+ fn testAllocating ( comptime alignment : std.mem.Alignment ) ! void {
2783+ var a : Allocating = .initAligned (testing .allocator , alignment );
27802784 defer a .deinit ();
27812785 const w = & a .writer ;
27822786
27832787 const x : i32 = 42 ;
27842788 const y : i32 = 1234 ;
27852789 try w .print ("x: {}\n y: {}\n " , .{ x , y });
2790+ const expected = "x: 42\n y: 1234\n " ;
2791+ try testing .expectEqualSlices (u8 , expected , a .written ());
2792+
2793+ // exercise *Aligned methods
2794+ var l = a .toArrayListAligned (alignment );
2795+ defer l .deinit (testing .allocator );
2796+ try testing .expectEqualSlices (u8 , expected , l .items );
2797+ a = .fromArrayListAligned (testing .allocator , alignment , & l );
2798+ try testing .expectEqualSlices (u8 , expected , a .written ());
2799+ const slice : []align (alignment .toByteUnits ()) u8 = @alignCast (try a .toOwnedSlice ());
2800+ try testing .expectEqualSlices (u8 , expected , slice );
2801+ a = .initOwnedSliceAligned (testing .allocator , alignment , slice );
2802+ try testing .expectEqualSlices (u8 , expected , a .writer .buffer );
2803+ }
27862804
2787- try testing .expectEqualSlices (u8 , "x: 42\n y: 1234\n " , a .written ());
2805+ test Allocating {
2806+ try testAllocating (.fromByteUnits (1 ));
2807+ try testAllocating (.fromByteUnits (4 ));
2808+ try testAllocating (.fromByteUnits (8 ));
2809+ try testAllocating (.fromByteUnits (16 ));
2810+ try testAllocating (.fromByteUnits (32 ));
2811+ try testAllocating (.fromByteUnits (64 ));
27882812 }
27892813};
27902814
0 commit comments