| ... | @@ -2541,13 +2541,17 @@ pub const Allocating = struct { | ... | @@ -2541,13 +2541,17 @@ pub const Allocating = struct { |
| 2541 | alignment: std.mem.Alignment, | 2541 | alignment: std.mem.Alignment, |
| 2542 | | 2542 | |
| 2543 | pub fn init(allocator: Allocator) Allocating { | 2543 | 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 { |
| 2544 | return .{ | 2548 | return .{ |
| 2545 | .allocator = allocator, | 2549 | .allocator = allocator, |
| 2546 | .writer = .{ | 2550 | .writer = .{ |
| 2547 | .buffer = &.{}, | 2551 | .buffer = &.{}, |
| 2548 | .vtable = &vtable, | 2552 | .vtable = &vtable, |
| 2549 | }, | 2553 | }, |
| 2550 | .alignment = .of(u8), | 2554 | .alignment = alignment, |
| 2551 | }; | 2555 | }; |
| 2552 | } | 2556 | } |
| 2553 | | 2557 | |
| ... | @@ -2775,16 +2779,36 @@ pub const Allocating = struct { | ... | @@ -2775,16 +2779,36 @@ pub const Allocating = struct { |
| 2775 | a.ensureUnusedCapacity(minimum_len) catch return error.WriteFailed; | 2779 | a.ensureUnusedCapacity(minimum_len) catch return error.WriteFailed; |
| 2776 | } | 2780 | } |
| 2777 | | 2781 | |
| 2778 | test Allocating { | 2782 | fn testAllocating(comptime alignment: std.mem.Alignment) !void { |
| 2779 | var a: Allocating = .init(testing.allocator); | 2783 | var a: Allocating = .initAligned(testing.allocator, alignment); |
| 2780 | defer a.deinit(); | 2784 | defer a.deinit(); |
| 2781 | const w = &a.writer; | 2785 | const w = &a.writer; |
| 2782 | | 2786 | |
| 2783 | const x: i32 = 42; | 2787 | const x: i32 = 42; |
| 2784 | const y: i32 = 1234; | 2788 | const y: i32 = 1234; |
| 2785 | try w.print("x: {}\ny: {}\n", .{ x, y }); | 2789 | try w.print("x: {}\ny: {}\n", .{ x, y }); |
| | 2790 | const expected = "x: 42\ny: 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 | } |
| 2786 | | 2804 | |
| 2787 | try testing.expectEqualSlices(u8, "x: 42\ny: 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)); |
| 2788 | } | 2812 | } |
| 2789 | }; | 2813 | }; |
| 2790 | | 2814 | |