tiny.preserves.CowSignedInteger
Defined in atom.
An integer with a tag that says whether its holder owns the integer's heap digits.
API (6)
Actions
Public operations.
borrow: Wrapsvalueas borrowed.deinit: Frees the integer's heap digits withallocatorwhen they are owned, and leaves them when they are borrowed.intoOwned: Returns a copy that owns its digits.own: Wrapsvalueas owned.
Fields and members
Public fields and members.
Source
Source: lib/preserves/src/atom.zig:94
zig
/// An integer with a tag that says whether its holder owns the integer's heap digits. Code that/// keeps an integer apart from its value holds it in this type, so the ownership of its digits/// travels with it. A new one borrows unless its builder says otherwise.pub const CowSignedInteger = struct { /// The integer. value: SignedInteger, /// Whether the holder owns the digits of `value`. It defaults to borrowed, so a struct written /// by hand never frees digits it did not allocate. ownership: Ownership = .borrowed, /// Wraps `value` as borrowed. Code that wraps an integer whose digits live elsewhere calls it. /// `deinit` leaves its digits alone. pub fn borrow(value: SignedInteger) CowSignedInteger { return .{ .value = value, .ownership = .borrowed }; } /// Wraps `value` as owned. Code that wraps an integer it built calls it. `deinit` frees its /// digits with the allocator it is given, so they have to come from that allocator. pub fn own(value: SignedInteger) CowSignedInteger { return .{ .value = value, .ownership = .owned }; } /// Frees the integer's heap digits with `allocator` when they are owned, and leaves them when /// they are borrowed. Code that holds the integer calls it when it is done with it. Only an /// integer too large for 128 bits has heap digits. The struct is undefined afterward. pub fn deinit(self: *CowSignedInteger, allocator: Allocator) void { if (self.ownership == .owned) self.value.deinit(allocator); self.* = undefined; } /// Returns a copy that owns its digits. Code that must keep the integer after its owner goes /// calls it. A borrowed integer is copied into storage from `allocator`. An owned integer comes /// back as the same struct with no copy, so the result and the original share its digits and /// only one of the two may be freed. pub fn intoOwned(self: CowSignedInteger, allocator: Allocator) !CowSignedInteger { return switch (self.ownership) { .owned => self, .borrowed => CowSignedInteger.own(try self.value.clone(allocator)), }; }};Source: lib/preserves/src/root.zig:132
zig
pub const CowSignedInteger = atom.CowSignedInteger;Also reachable as
Audit
| Definitions | 5 |
|---|---|
| Public names | 15 |
| Members | 2 |
| Version | 26.7.0 |
| Revision | daab053ee433 |