Initial import: Aji 0.1 draft spec and Zig scaffolding
Draft specification, RFCs, reference registries and examples, plus the reference-implementation skeleton: build.zig, diagnostic envelope with error-code registry synced to docs, aji CLI, conformance runner with seed tests, CI and contributor docs.
This commit is contained in:
commit
17a55a7fef
67 files changed
+4197
No files matched your search
@@ -0,0 +1,52 @@
|
||||
module examples.append_log;
|
||||
|
||||
const HEADER_SIZE: usize = 16;
|
||||
|
||||
struct Header {
|
||||
len: u32,
|
||||
crc: u32,
|
||||
sequence: u64,
|
||||
}
|
||||
|
||||
struct Log {
|
||||
file: File,
|
||||
offset: u64,
|
||||
}
|
||||
|
||||
fn encode_header(header: ref<Header>) -> [u8; HEADER_SIZE]
|
||||
effects []
|
||||
{
|
||||
var out = [0u8; HEADER_SIZE];
|
||||
bytes.put_u32_le(mut out, 0, header.len);
|
||||
bytes.put_u32_le(mut out, 4, header.crc);
|
||||
bytes.put_u64_le(mut out, 8, header.sequence);
|
||||
return out;
|
||||
}
|
||||
|
||||
fn append(
|
||||
log: mut<Log>,
|
||||
data: slice<u8>
|
||||
) -> Result<u64, AppendError>
|
||||
requires {
|
||||
len(data) <= 16 MiB
|
||||
}
|
||||
effects [fs.write]
|
||||
{
|
||||
let crc = crc32c(data);
|
||||
let len32 = checked_cast<u32>(len(data))?;
|
||||
|
||||
let header = Header {
|
||||
len: len32,
|
||||
crc: crc,
|
||||
sequence: log.offset,
|
||||
};
|
||||
let encoded = encode_header(ref header);
|
||||
|
||||
fs.write_all(ref log.file, slice.of(encoded))?;
|
||||
fs.write_all(ref log.file, data)?;
|
||||
|
||||
let old = log.offset;
|
||||
log.offset = checked_add(log.offset, HEADER_SIZE + len(data))?;
|
||||
|
||||
return Ok(old);
|
||||
}
|
||||
Reference in new issue
Block a user