tiny.preserves.Atom
Defined in atom.
A copy of one atom, tagged by its kind.
API (20)
Actions
Public operations.
class: Returns the atom's kind.deinit: Frees the atom's bytes or digits withallocatorwhen it owns them.eql: Returns whether two atoms have the same kind and the same contents, whatever their ownership tags.fromBool: Makes a boolean atom fromv.fromByteStringBorrowed: Makes a byte-string atom that borrowsbytes.fromByteStringOwned: Makes a byte-string atom that ownsbytes, so itsdeinitfrees them.fromDouble: Makes a double atom fromv.fromSignedIntegerBorrowed: Makes an integer atom that borrowsv's digits.fromSignedIntegerOwned: Makes an integer atom that ownsv's digits, so itsdeinitfrees them.fromStringBorrowed: Makes a string atom that borrowsbytes.fromStringOwned: Makes a string atom that ownsbytes, so itsdeinitfrees them.fromSymbolBorrowed: Makes a symbol atom that borrowsbytes.fromSymbolOwned: Makes a symbol atom that ownsbytes, so itsdeinitfrees them.intoOwned: Returns a copy of the atom that owns its bytes or digits, copying borrowed ones into storage fromallocator.
Fields and members
Public fields and members.
Source
Source: lib/preserves/src/atom.zig:137
zig
/// A copy of one atom, tagged by its kind. Code that takes one atom out of a value holds it in this/// type, as `Value.asAtom` returns it. Booleans and doubles are stored directly. Strings, byte/// strings and symbols are stored as bytes with an ownership tag, and integers as an integer with/// one. `Value.asAtom` builds one that borrows from a value.pub const Atom = union(AtomClass) { /// The boolean. boolean: bool, /// The double. double: f64, /// The integer, with whether its holder owns its digits. signed_integer: CowSignedInteger, /// The string's bytes, with whether its holder owns them. string: CowBytes, /// The byte string's bytes, with whether its holder owns them. byte_string: CowBytes, /// The symbol's name bytes, with whether its holder owns them. symbol: CowBytes, /// Returns the atom's kind. Code that switches on an atom's kind calls it. pub fn class(self: Atom) AtomClass { return @as(AtomClass, self); } /// Makes a boolean atom from `v`. `Value.asAtom` calls it for a boolean atom. pub fn fromBool(v: bool) Atom { return .{ .boolean = v }; } /// Makes a double atom from `v`. `Value.asAtom` calls it for a double atom. pub fn fromDouble(v: f64) Atom { return .{ .double = v }; } /// Makes an integer atom that borrows `v`'s digits. `Value.asAtom` calls it for an integer atom /// over the value's own digits. pub fn fromSignedIntegerBorrowed(v: SignedInteger) Atom { return .{ .signed_integer = CowSignedInteger.borrow(v) }; } /// Makes an integer atom that owns `v`'s digits, so its `deinit` frees them. Code that hands an /// integer's digits to the atom calls it. pub fn fromSignedIntegerOwned(v: SignedInteger) Atom { return .{ .signed_integer = CowSignedInteger.own(v) }; } /// Makes a string atom that borrows `bytes`. `Value.asAtom` calls it for a string atom over the /// value's own bytes. pub fn fromStringBorrowed(bytes: []const u8) Atom { return .{ .string = CowBytes.borrow(bytes) }; } /// Makes a string atom that owns `bytes`, so its `deinit` frees them. Code that hands a /// string's bytes to the atom calls it. pub fn fromStringOwned(bytes: []const u8) Atom { return .{ .string = CowBytes.own(bytes) }; } /// Makes a byte-string atom that borrows `bytes`. `Value.asAtom` calls it for a byte-string /// atom over the value's own bytes. pub fn fromByteStringBorrowed(bytes: []const u8) Atom { return .{ .byte_string = CowBytes.borrow(bytes) }; } /// Makes a byte-string atom that owns `bytes`, so its `deinit` frees them. Code that hands a /// byte string's bytes to the atom calls it. pub fn fromByteStringOwned(bytes: []const u8) Atom { return .{ .byte_string = CowBytes.own(bytes) }; } /// Makes a symbol atom that borrows `bytes`. `Value.asAtom` calls it for a symbol atom over the /// value's own bytes. pub fn fromSymbolBorrowed(bytes: []const u8) Atom { return .{ .symbol = CowBytes.borrow(bytes) }; } /// Makes a symbol atom that owns `bytes`, so its `deinit` frees them. Code that hands a /// symbol's bytes to the atom calls it. pub fn fromSymbolOwned(bytes: []const u8) Atom { return .{ .symbol = CowBytes.own(bytes) }; } /// Frees the atom's bytes or digits with `allocator` when it owns them. Code that holds the /// atom calls it when it is done with it. Booleans and doubles need nothing. The atom is /// undefined afterward. pub fn deinit(self: *Atom, allocator: Allocator) void { switch (self.*) { .boolean, .double => {}, .signed_integer => |*c| c.deinit(allocator), .string => |*c| c.deinit(allocator), .byte_string => |*c| c.deinit(allocator), .symbol => |*c| c.deinit(allocator), } self.* = undefined; } /// Returns a copy of the atom that owns its bytes or digits, copying borrowed ones into storage /// from `allocator`. Code that keeps an atom calls it so the atom outlives the value it came /// from. Parts that are already owned come back with no copy, so the result shares them with /// the original and only one of the two may be freed. pub fn intoOwned(self: Atom, allocator: Allocator) !Atom { return switch (self) { .boolean, .double => self, .signed_integer => |c| .{ .signed_integer = try c.intoOwned(allocator) }, .string => |c| .{ .string = try c.intoOwned(allocator) }, .byte_string => |c| .{ .byte_string = try c.intoOwned(allocator) }, .symbol => |c| .{ .symbol = try c.intoOwned(allocator) }, }; } /// Returns whether two atoms have the same kind and the same contents, whatever their ownership /// tags. Code that compares atoms from different owners calls it, so ownership plays no part. /// Doubles are equal when their bit patterns are equal, so a `NaN` equals a `NaN` with the same /// bits and `0.0` differs from `-0.0`. Integers compare with `SignedInteger.eql`. pub fn eql(a: Atom, b: Atom) bool { if (a.class() != b.class()) return false; return switch (a) { .boolean => |v| v == b.boolean, .double => |v| @as(u64, @bitCast(v)) == @as(u64, @bitCast(b.double)), .signed_integer => |c| c.value.eql(b.signed_integer.value), .string => |c| std.mem.eql(u8, c.bytes, b.string.bytes), .byte_string => |c| std.mem.eql(u8, c.bytes, b.byte_string.bytes), .symbol => |c| std.mem.eql(u8, c.bytes, b.symbol.bytes), }; }};Source: lib/preserves/src/root.zig:130
zig
pub const Atom = atom.Atom;Also reachable as
Audit
| Definitions | 15 |
|---|---|
| Public names | 45 |
| Members | 6 |
| Version | 26.7.0 |
| Revision | daab053ee433 |