| ... | @@ -87,6 +87,17 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type { | ... | @@ -87,6 +87,17 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type { |
| 87 | }; | 87 | }; |
| 88 | } | 88 | } |
| 89 | | 89 | |
| | 90 | /// ArrayList takes ownership of the passed in slice. The slice must have been |
| | 91 | /// allocated with `allocator`. |
| | 92 | /// Deinitialize with `deinit` or use `toOwnedSlice`. |
| | 93 | pub fn fromOwnedSliceSentinel(allocator: Allocator, comptime sentinel: T, slice: [:sentinel]T) Self { |
| | 94 | return Self{ |
| | 95 | .items = slice, |
| | 96 | .capacity = slice.len + 1, |
| | 97 | .allocator = allocator, |
| | 98 | }; |
| | 99 | } |
| | 100 | |
| 90 | /// Initializes an ArrayListUnmanaged with the `items` and `capacity` fields | 101 | /// Initializes an ArrayListUnmanaged with the `items` and `capacity` fields |
| 91 | /// of this ArrayList. Empties this ArrayList. | 102 | /// of this ArrayList. Empties this ArrayList. |
| 92 | pub fn moveToUnmanaged(self: *Self) ArrayListAlignedUnmanaged(T, alignment) { | 103 | pub fn moveToUnmanaged(self: *Self) ArrayListAlignedUnmanaged(T, alignment) { |
| ... | @@ -546,6 +557,26 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ | ... | @@ -546,6 +557,26 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ |
| 546 | return .{ .items = self.items, .capacity = self.capacity, .allocator = allocator }; | 557 | return .{ .items = self.items, .capacity = self.capacity, .allocator = allocator }; |
| 547 | } | 558 | } |
| 548 | | 559 | |
| | 560 | /// ArrayListUnmanaged takes ownership of the passed in slice. The slice must have been |
| | 561 | /// allocated with `allocator`. |
| | 562 | /// Deinitialize with `deinit` or use `toOwnedSlice`. |
| | 563 | pub fn fromOwnedSlice(slice: Slice) Self { |
| | 564 | return Self{ |
| | 565 | .items = slice, |
| | 566 | .capacity = slice.len, |
| | 567 | }; |
| | 568 | } |
| | 569 | |
| | 570 | /// ArrayListUnmanaged takes ownership of the passed in slice. The slice must have been |
| | 571 | /// allocated with `allocator`. |
| | 572 | /// Deinitialize with `deinit` or use `toOwnedSlice`. |
| | 573 | pub fn fromOwnedSliceSentinel(comptime sentinel: T, slice: [:sentinel]T) Self { |
| | 574 | return Self{ |
| | 575 | .items = slice, |
| | 576 | .capacity = slice.len + 1, |
| | 577 | }; |
| | 578 | } |
| | 579 | |
| 549 | /// The caller owns the returned memory. Empties this ArrayList. | 580 | /// The caller owns the returned memory. Empties this ArrayList. |
| 550 | /// Its capacity is cleared, making deinit() safe but unnecessary to call. | 581 | /// Its capacity is cleared, making deinit() safe but unnecessary to call. |
| 551 | pub fn toOwnedSlice(self: *Self, allocator: Allocator) Allocator.Error!Slice { | 582 | pub fn toOwnedSlice(self: *Self, allocator: Allocator) Allocator.Error!Slice { |
| ... | @@ -1564,6 +1595,45 @@ test "std.ArrayList/ArrayListUnmanaged.addManyAsArray" { | ... | @@ -1564,6 +1595,45 @@ test "std.ArrayList/ArrayListUnmanaged.addManyAsArray" { |
| 1564 | } | 1595 | } |
| 1565 | } | 1596 | } |
| 1566 | | 1597 | |
| | 1598 | test "std.ArrayList/ArrayList.fromOwnedSliceSentinel" { |
| | 1599 | const a = testing.allocator; |
| | 1600 | |
| | 1601 | var orig_list = ArrayList(u8).init(a); |
| | 1602 | defer orig_list.deinit(); |
| | 1603 | try orig_list.appendSlice("foobar"); |
| | 1604 | const sentinel_slice = try orig_list.toOwnedSliceSentinel(0); |
| | 1605 | |
| | 1606 | var list = ArrayList(u8).fromOwnedSliceSentinel(a, 0, sentinel_slice); |
| | 1607 | defer list.deinit(); |
| | 1608 | try testing.expectEqualStrings(list.items, "foobar"); |
| | 1609 | } |
| | 1610 | |
| | 1611 | test "std.ArrayList/ArrayListUnmanaged.fromOwnedSlice" { |
| | 1612 | const a = testing.allocator; |
| | 1613 | |
| | 1614 | var list = ArrayList(u8).init(a); |
| | 1615 | defer list.deinit(); |
| | 1616 | try list.appendSlice("foobar"); |
| | 1617 | |
| | 1618 | const slice = try list.toOwnedSlice(); |
| | 1619 | var unmanaged = ArrayListUnmanaged(u8).fromOwnedSlice(slice); |
| | 1620 | defer unmanaged.deinit(a); |
| | 1621 | try testing.expectEqualStrings(unmanaged.items, "foobar"); |
| | 1622 | } |
| | 1623 | |
| | 1624 | test "std.ArrayList/ArrayListUnmanaged.fromOwnedSliceSentinel" { |
| | 1625 | const a = testing.allocator; |
| | 1626 | |
| | 1627 | var list = ArrayList(u8).init(a); |
| | 1628 | defer list.deinit(); |
| | 1629 | try list.appendSlice("foobar"); |
| | 1630 | |
| | 1631 | const sentinel_slice = try list.toOwnedSliceSentinel(0); |
| | 1632 | var unmanaged = ArrayListUnmanaged(u8).fromOwnedSliceSentinel(0, sentinel_slice); |
| | 1633 | defer unmanaged.deinit(a); |
| | 1634 | try testing.expectEqualStrings(unmanaged.items, "foobar"); |
| | 1635 | } |
| | 1636 | |
| 1567 | test "std.ArrayList/ArrayListUnmanaged.toOwnedSliceSentinel" { | 1637 | test "std.ArrayList/ArrayListUnmanaged.toOwnedSliceSentinel" { |
| 1568 | const a = testing.allocator; | 1638 | const a = testing.allocator; |
| 1569 | { | 1639 | { |