Hello.
I'm porting a pet-project to Zig, I originally prototyped it in ES6 a few years back.
But i'm a zig-noob so I struggle with the main interface design.
The problem:
I made the feed as an in-memory chain of blocks, so the entire construct was just a single readable/writable buffer.
This used to be my ES6-workflow:
const { sk: aliceKey } = Feed.signPair()
const { sk: bobKey } = Feed.signPair()
const a = new Feed()
a.append('Hello', aliceKey);
a.append('World', aliceKey);
a.length // => 2
const b = new Feed()
b.merge(a) // length is now 2
b.append('Hey Alice! /B', bobKey);
b.length // => 3
a.merge(b)
a.length // => 3
It's ok but wastes a lot of resources behind the scenes.
In Zig I implemented the Feed decoder as a simple Iterator, it was real nice.
But It also made me start thinking about "readonly" feeds since zig has real a pretty powerful const.
So now I'm stuck with a struct that tries to squeeze 2 concepts into a single field..
🦶
pub const Variant = enum { readable, writable };
pub const Feed = struct {
buffer: union(Variant) {
/// Read-only feed
readable: []u8,
/// Backed by arraylist with growing capacity.
writable: ArrayList(u8),
},
}
Are there any better patterns i could use?
Help 🙏