Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ on:
branches: [main]

env:
ZIG_VERSION: 0.15.2
ZIG_VERSION: 0.16.0

jobs:
build:
Expand Down
2 changes: 1 addition & 1 deletion build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

.version = "0.1.0",

.minimum_zig_version = "0.15.2",
.minimum_zig_version = "0.16.0",

.dependencies = .{},
.paths = .{
Expand Down
14 changes: 7 additions & 7 deletions src/cid.zig
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ pub fn CID(comptime S: usize) type {

if (version == 0x12 and codec == 0x20) {
var digest: [32]u8 = undefined;
try reader.readNoEof(&digest);
try reader.readSliceAll(&digest);
const version_codec = try Multicodec.fromCode(version);
const mh = try Multihash(S).wrap(version_codec, &digest);
return newV0(mh);
Expand Down Expand Up @@ -217,9 +217,9 @@ pub fn CID(comptime S: usize) type {

/// Converts the CID to a byte slice.
pub fn toBytes(self: *const Self, dest: []u8) ![]u8 {
var stream = std.io.fixedBufferStream(dest);
var w = std.Io.Writer.fixed(dest);

const written = try self.writeStream(stream.writer());
const written = try self.writeStream(&w);
return dest[0..written];
}

Expand Down Expand Up @@ -263,8 +263,8 @@ pub fn CID(comptime S: usize) type {
}

pub fn fromBytes(bytes: []const u8) !Self {
var fbs = std.io.fixedBufferStream(bytes);
return try Self.readStream(fbs.reader());
var r = std.Io.Reader.fixed(bytes);
return try Self.readStream(&r);
}

pub fn fromString(codec: MultiBaseCodec, dest: []u8, cid_str: []const u8) !Self {
Expand Down Expand Up @@ -365,8 +365,8 @@ test CID {
const bytes = try original.toBytes(buffer);
defer allocator.free(buffer);

var fbs = std.io.fixedBufferStream(bytes);
const decoded = try CID(32).readStream(fbs.reader());
var r = std.Io.Reader.fixed(bytes);
const decoded = try CID(32).readStream(&r);

try testing.expect(original.isEqual(&decoded));
}
Expand Down
13 changes: 7 additions & 6 deletions src/multibase.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2608,11 +2608,12 @@ test "Base58Flickr decode large data" {
}

fn loadTestData(allocator: std.mem.Allocator, filePath: []const u8) ![]const u8 {
var file = try std.fs.cwd().openFile(filePath, .{ .mode = std.fs.File.OpenMode.read_only });
defer file.close();
const fileSize = try file.getEndPos();
const io = std.testing.io;
var file = try std.Io.Dir.cwd().openFile(io, filePath, .{ .mode = .read_only });
defer file.close(io);
const fileSize = try file.length(io);
const buffer = try allocator.alloc(u8, fileSize);
_ = try file.readAll(buffer);
const content: []const u8 = buffer;
return content;
var file_reader = file.reader(io, &.{});
try file_reader.interface.readSliceAll(buffer);
return buffer;
}
46 changes: 25 additions & 21 deletions src/multibase_bench.zig
Original file line number Diff line number Diff line change
Expand Up @@ -80,24 +80,28 @@ fn call_decode(codec: multibase.MultiBaseCodec, dest: []u8, data: []const u8) vo

const bench_data_path = "./test/data/bench_data";

pub fn main() !void {
pub fn main(init: std.process.Init) !void {
const allocator = std.heap.page_allocator;
const bench_data = try loadBenchData(allocator, bench_data_path);
const config = try parse_args();
var bench_res = try run_bench(bench_data, config);
try bench_res.format(std.io.getStdOut().writer());
const io = init.io;

const bench_data = try loadBenchData(io, allocator, bench_data_path);
const config = try parse_args(init);
var bench_res = try run_bench(io, bench_data, config);

var stdout_buf: [4096]u8 = undefined;
var stdout_writer = std.Io.File.stdout().writer(io, &stdout_buf);
try bench_res.format(&stdout_writer.interface);
try stdout_writer.interface.flush();
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The std.Io.Writer interface does not contain a flush method. Since stdout_writer is a bound buffered writer, you should call flush() directly on the writer object itself, rather than through its interface field.

    try stdout_writer.flush();

}

fn parse_args() !BenchConfig {
fn parse_args(init: std.process.Init) !BenchConfig {
var period: u64 = 10;
var times: u64 = 1_000_000;
var code: ?multibase.MultiBaseCodec = null;
var method: []const u8 = "encode";

var arena_state = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer arena_state.deinit();
const arena = arena_state.allocator();
const args = try std.process.argsAlloc(arena);
const arena = init.arena.allocator();
const args = try init.minimal.args.toSlice(arena);

var i: usize = 1;
while (i < args.len) : (i += 1) {
Expand Down Expand Up @@ -165,7 +169,7 @@ fn handle_method(value: []const u8) ![]const u8 {
}
}

fn run_bench(bench_data: []const u8, config: BenchConfig) !BenchResult {
fn run_bench(io: std.Io, bench_data: []const u8, config: BenchConfig) !BenchResult {
var dest_len = config.code.encodedLen(bench_data);
var action = Func{
.fn_ptr = call_encode,
Expand All @@ -189,13 +193,13 @@ fn run_bench(bench_data: []const u8, config: BenchConfig) !BenchResult {
var time_records = try std.heap.page_allocator.alloc(i128, config.period);
defer std.heap.page_allocator.free(time_records);
while (i < config.period) : (i += 1) {
const start_time = std.time.nanoTimestamp();
const start_time = std.Io.Clock.awake.now(io).toNanoseconds();
for (0..config.times) |_| {
action.call(dest, data);
// _ = config.code.encode(dest, bench_data);
}
const end_time = std.time.nanoTimestamp();
const elapsed_time = end_time - start_time;
const end_time = std.Io.Clock.awake.now(io).toNanoseconds();
const elapsed_time: i128 = end_time - start_time;
time_records[i] = elapsed_time;
if (elapsed_time > max_time) {
max_time = elapsed_time;
Expand All @@ -222,12 +226,12 @@ fn run_bench(bench_data: []const u8, config: BenchConfig) !BenchResult {
};
}

fn loadBenchData(allocator: std.mem.Allocator, filePath: []const u8) ![]const u8 {
var file = try std.fs.cwd().openFile(filePath, .{ .mode = std.fs.File.OpenMode.read_only });
defer file.close();
const fileSize = try file.getEndPos();
fn loadBenchData(io: std.Io, allocator: std.mem.Allocator, filePath: []const u8) ![]const u8 {
var file = try std.Io.Dir.cwd().openFile(io, filePath, .{ .mode = .read_only });
defer file.close(io);
const fileSize = try file.length(io);
const buffer = try allocator.alloc(u8, fileSize);
_ = try file.readAll(buffer);
const content: []const u8 = buffer;
return content;
var file_reader = file.reader(io, &.{});
try file_reader.interface.readSliceAll(buffer);
return buffer;
}
18 changes: 9 additions & 9 deletions src/multihash.zig
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ pub fn Multihash(comptime S: usize) type {

/// readBytes reads a Multihash from a byte slice.
pub fn readBytes(bytes: []const u8) !Self {
var stream = std.io.fixedBufferStream(bytes);
return try Self.read(stream.reader());
var r = std.Io.Reader.fixed(bytes);
return try Self.read(&r);
}

/// read reads a Multihash from a reader.
Expand All @@ -110,7 +110,7 @@ pub fn Multihash(comptime S: usize) type {
}

var digest = [_]u8{0} ** S;
try reader.readNoEof(digest[0..size]);
try reader.readSliceAll(digest[0..size]);

return Self{
.code = try Multicodec.fromCode(code),
Expand All @@ -121,8 +121,8 @@ pub fn Multihash(comptime S: usize) type {

/// toBytes converts the Multihash to a byte slice.
pub fn toBytes(self: *const Self, dest: []u8) ![]const u8 {
var stream = std.io.fixedBufferStream(dest);
const written = try self.write(stream.writer());
var w = std.Io.Writer.fixed(dest);
const written = try self.write(&w);
return dest[0..written];
}
};
Expand Down Expand Up @@ -640,16 +640,16 @@ test "multihash serialization" {
var mh = try Multihash(32).wrap(Multicodec.SHA2_256, &[_]u8{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });

var buf: [100]u8 = undefined;
var fbs = std.io.fixedBufferStream(&buf);
const written = try mh.write(fbs.writer());
var w = std.Io.Writer.fixed(&buf);
const written = try mh.write(&w);
try testing.expectEqual(written, expected_bytes.len);
try testing.expectEqualSlices(u8, buf[0..written], &expected_bytes);
}

test "multihash deserialization" {
const input = [_]u8{ 0x12, 0x0a, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
var fbs = std.io.fixedBufferStream(&input);
var mh = try Multihash(32).read(fbs.reader());
var r = std.Io.Reader.fixed(&input);
var mh = try Multihash(32).read(&r);

try testing.expectEqual(mh.getCode().getCode(), 0x12);
try testing.expectEqual(mh.getSize(), 10);
Expand Down
2 changes: 1 addition & 1 deletion src/root.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ pub const multibase = @import("multibase.zig");
pub const cid = @import("cid.zig");

test {
@import("std").testing.refAllDeclsRecursive(@This());
@import("std").testing.refAllDecls(@This());
}
16 changes: 8 additions & 8 deletions src/unsigned_varint.zig
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ pub fn decodeStream(reader: anytype, comptime T: type) !T {
var continuation_bytes: usize = 0;

while (true) {
const byte = try reader.readByte();
const byte = try reader.takeByte();

if (!isLast(byte)) {
continuation_bytes += 1;
Expand Down Expand Up @@ -239,21 +239,21 @@ test "specific_values" {
}

test "stream_identity" {
var buf: std.ArrayList(u8) = .{};
defer buf.deinit(testing.allocator);
var aw: std.Io.Writer.Allocating = .init(testing.allocator);
defer aw.deinit();

const numbers = [_]u64{ 1, 127, 128, 255, 300, 16384 };

for (numbers) |n| {
// Encode to stream
const written = try encodeStream(buf.writer(testing.allocator), u64, n);
try testing.expectEqual(written, encode(u64, n, buf.items[0..]).len);
const written = try encodeStream(&aw.writer, u64, n);
try testing.expectEqual(written, aw.written().len);

// Decode from stream
var stream = std.io.fixedBufferStream(buf.items);
const decoded = try decodeStream(stream.reader(), u64);
var r = std.Io.Reader.fixed(aw.written());
const decoded = try decodeStream(&r, u64);

try testing.expectEqual(n, decoded);
buf.clearRetainingCapacity();
aw.clearRetainingCapacity();
}
}
Loading