tiny.preserves.Constructors
Defined in constructors_mod.
Returns a namespace of constructors for Value(D), the Preserves value whose embedded values hold a D.
Source
Source: lib/preserves/src/constructors.zig:33
zig
/// Returns a namespace of constructors for `Value(D)`, the Preserves value whose embedded values/// hold a `D`. A caller whose values hold embedded values of one type calls it once at compile/// time, for every constructor over those values. The JSON decoder, the protocol record builders/// and the pattern conversions each call it to build their records. `D` has to provide `eql`,/// `order`, `deinit` and `clone`, or the call is a compile error.pub fn Constructors(comptime D: type) type { domain_mod.assertIsDomain(D); const V = Value(D); return struct { /// Returns the boolean value `v`. The call allocates nothing. pub fn boolean(v: bool) V { return V.initBoolean(v); } /// Returns an integer value holding `v`. The call widens `v` to 128 bits, so the value /// compares and encodes like any other integer. The call allocates nothing. pub fn integer(v: i64) V { return V.initI128(@as(i128, v)); } /// Returns the double value `v`. The call allocates nothing. pub fn float(v: f64) V { return V.initDouble(v); } /// Returns a string value that borrows the bytes `s`. The value points into `s`, so `s` has /// to stay alive while the value is in use. The call checks nothing about `s`, including /// whether it is valid UTF-8. pub fn string(s: []const u8) V { return .{ .string = s }; } /// Returns a symbol value that borrows the bytes `name`. The value points into `name`, so /// `name` has to stay alive while the value is in use. pub fn symbol(name: []const u8) V { return .{ .symbol = name }; } /// Returns the discard pattern, which matches any value. The call allocates nothing. pub fn discard() V { return .{ .discard = {} }; } /// Returns the symbol `null`, the value the package uses for JSON's null. The symbol's /// bytes are a constant of the package, so the value is safe to keep. `isNull` recognizes /// the value. pub fn null_val() V { return .{ .symbol = symbols_mod.SYM_NULL.name }; } /// Returns a capture pattern, which matches what `inner` matches and records the matched /// value. The call allocates one cell for `inner` with `alloc` and moves `inner` into it. /// The only error is running out of memory. pub fn capture(alloc: Allocator, inner: V) !V { const p = try alloc.create(V); p.* = inner; return .{ .capture = p }; } /// Returns a pattern that matches what `inner` matches and records the matched value under /// `name`. The call allocates one cell for `inner` with `alloc` and borrows `name`. On /// failure the call frees its cell and leaves `inner` with the caller. pub fn bindVal(alloc: Allocator, name: []const u8, inner: V) !V { const p = try alloc.create(V); errdefer alloc.destroy(p); p.* = inner; return .{ .bind = .{ .name = name, .pattern = p } }; } /// Returns a pattern that matches a sequence whose first items match `prefix` and whose /// remaining items match `rest`. The call copies the `prefix` slice and allocates one cell /// for `rest`, both with `alloc`. The copy is shallow: the items it holds still point to /// whatever the caller's items pointed to. On failure the call frees what it allocated and /// leaves `prefix` and `rest` with the caller. pub fn restPattern(alloc: Allocator, prefix: []const V, rest: V) !V { const prefix_copy = try alloc.dupe(V, prefix); errdefer alloc.free(prefix_copy); const rest_ptr = try alloc.create(V); errdefer alloc.destroy(rest_ptr); rest_ptr.* = rest; return .{ .rest_pattern = .{ .prefix = prefix_copy, .rest = rest_ptr } }; } /// Returns an embedded value that holds the pointer `ptr`. The call is a compile error /// unless `D` is `AnyEmbedded`. The value carries no equality, cleanup or copy functions, /// so two such values are equal only when they hold the same pointer. Freeing the value /// leaves the pointed-to data alone. pub fn embedded(ptr: *anyopaque) V { if (D != AnyEmbedded) { @compileError("Constructors(" ++ @typeName(D) ++ ").embedded(ptr) requires D == AnyEmbedded"); } return V{ .embedded = AnyEmbedded{ .value = ptr } }; } /// Returns a record with label `label` and fields `fields`. The call allocates one cell for /// the label and a copy of the `fields` slice, both with `alloc`. The copy is shallow: the /// new record holds the same field values the caller passed. `freeValue` frees exactly the /// cell and the slice this call allocated. On failure the call frees its cell and leaves /// `label` and `fields` with the caller. pub fn record(alloc: Allocator, label: V, fields: []const V) !V { const lp = try alloc.create(V); errdefer alloc.destroy(lp); lp.* = label; const fs = try alloc.dupe(V, fields); return .{ .record = .{ .label = lp, .fields = fs } }; } /// Returns a sequence holding a copy of the `items` slice, allocated with `alloc`. The copy /// is shallow: the new sequence holds the same item values the caller passed. `freeValue` /// frees exactly the slice this call allocated. pub fn sequence(alloc: Allocator, items: []const V) !V { const s = try alloc.dupe(V, items); return .{ .sequence = s }; } /// Returns a set holding a sorted copy of `items`, allocated with `alloc`. The copy is /// sorted by `compare`, the package's total order on values. The copy is shallow, and /// `freeValue` frees exactly the slice this call allocated. The duplicate check compares /// every pair of items, so its cost grows with the square of the count. The call returns /// `error.DuplicateSetElement` when two items are equal. That error allocates nothing. The /// duplicate check leaves `items` with the caller. pub fn set(alloc: Allocator, items: []const V) !V { if (!V.setElementsDistinct(items)) return error.DuplicateSetElement; const sorted = try alloc.dupe(V, items); const Cmp = struct { fn lt(_: void, a: V, b: V) bool { return a.compare(b) == .lt; } }; std.mem.sort(V, sorted, {}, Cmp.lt); return .{ .set = sorted }; } /// Returns a dictionary holding a copy of `entries` sorted by key, allocated with `alloc`. /// The keys are sorted by `compare`, the package's total order on values. The copy is /// shallow, and `freeValue` frees exactly the slice this call allocated. The duplicate /// check compares every pair of keys, so its cost grows with the square of the count. The /// call returns `error.DuplicateDictionaryKey` when two keys are equal. That error /// allocates nothing. The duplicate check leaves `entries` with the caller. pub fn dictionary(alloc: Allocator, entries: []const V.DictionaryEntry) !V { if (!V.dictionaryKeysDistinct(entries)) return error.DuplicateDictionaryKey; const sorted = try alloc.dupe(V.DictionaryEntry, entries); const Cmp = struct { fn lt(_: void, a: V.DictionaryEntry, b: V.DictionaryEntry) bool { return a.key.compare(b.key) == .lt; } }; std.mem.sort(V.DictionaryEntry, sorted, {}, Cmp.lt); return .{ .dictionary = sorted }; } };}Source: lib/preserves/src/root.zig:164
zig
pub const Constructors = constructors_mod.Constructors;Audit
| Definitions | 1 |
|---|---|
| Public names | 2 |
| Members | 0 |
| Version | 26.7.0 |
| Revision | daab053ee433 |