tiny.preserves.CowBytes
Defined in atom.
A byte slice with a tag that says whether its holder owns the bytes.
API (6)
Actions
Public operations.
borrow: Wrapsbytesas borrowed.deinit: Frees the bytes withallocatorwhen they are owned, and leaves them when they are borrowed.intoOwned: Returns a copy that owns its bytes.own: Wrapsbytesas owned.
Fields and members
Public fields and members.
Source
Source: lib/preserves/src/atom.zig:52
zig
/// A byte slice with a tag that says whether its holder owns the bytes. Code that keeps a string,/// byte string or symbol apart from its value holds it in this type, so its ownership travels with/// it. A new one borrows its bytes unless its builder says otherwise.pub const CowBytes = struct { /// The bytes of the string, byte string or symbol. bytes: []const u8, /// Whether the holder owns `bytes`. It defaults to borrowed, so a struct written by hand never /// frees bytes it did not allocate. ownership: Ownership = .borrowed, /// Wraps `bytes` as borrowed. Code that wraps bytes it keeps alive elsewhere calls it. `deinit` /// leaves the bytes alone, so they have to outlive the result. pub fn borrow(bytes: []const u8) CowBytes { return .{ .bytes = bytes, .ownership = .borrowed }; } /// Wraps `bytes` as owned. Code that wraps bytes it allocated calls it. `deinit` frees them /// with the allocator it is given, so they have to come from that allocator. pub fn own(bytes: []const u8) CowBytes { return .{ .bytes = bytes, .ownership = .owned }; } /// Frees the bytes with `allocator` when they are owned, and leaves them when they are /// borrowed. Code that holds the bytes calls it when it is done with them. The struct is /// undefined afterward. pub fn deinit(self: *CowBytes, allocator: Allocator) void { if (self.ownership == .owned) allocator.free(self.bytes); self.* = undefined; } /// Returns a copy that owns its bytes. Code that must keep the bytes after their owner goes /// calls it. Borrowed bytes are copied into storage from `allocator`. Owned bytes come back as /// the same struct with no copy, so the result and the original share them and only one of the /// two may be freed. The call can fail only with `error.OutOfMemory`. pub fn intoOwned(self: CowBytes, allocator: Allocator) !CowBytes { return switch (self.ownership) { .owned => self, .borrowed => CowBytes.own(try allocator.dupe(u8, self.bytes)), }; }};Source: lib/preserves/src/root.zig:131
zig
pub const CowBytes = atom.CowBytes;Also reachable as
Audit
| Definitions | 5 |
|---|---|
| Public names | 15 |
| Members | 2 |
| Version | 26.7.0 |
| Revision | daab053ee433 |