Skip to content

Commit

Permalink
Fixes for building with 0.13 (#1)
Browse files Browse the repository at this point in the history
* Fixes for building with 0.13

While at it, also make the library a module.

* Switch to GenericReader, Reader is deprecated

* Update build.zig

* Update build.zig

---------

Co-authored-by: Meghan Denny <hello@nektro.net>
  • Loading branch information
SanchayanMaity and nektro authored Nov 6, 2024
1 parent 628b7c3 commit 8a2e199
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 8 deletions.
19 changes: 16 additions & 3 deletions build.zig
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
const std = @import("std");

pub fn build(b: *std.build.Builder) void {
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const mode = b.option(std.builtin.Mode, "mode", "") orelse .Debug;

const torrent_file = b.path("archlinux-2021.04.01-x86_64.iso.torrent");

const bencode = b.addModule("bencode", .{
.root_source_file = b.path("src/lib.zig"),
.target = target,
.optimize = optimize,
});

const exe = b.addExecutable(.{
.name = "zig-bencode",
.root_source_file = .{ .path = "src/main.zig" },
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = mode,
.optimize = optimize,
});
exe.root_module.addImport("bencode", bencode);

exe.root_module.addAnonymousImport("torrent_file", .{
.root_source_file = torrent_file,
});

b.installArtifact(exe);
Expand Down
12 changes: 12 additions & 0 deletions build.zig.zon
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
.{
.name = "bencode",
.version = "0.0.0",
.dependencies = .{},
.paths = .{
"build.zig",
"build.zig.zon",
"README.md",
"LICENSE",
"src",
},
}
4 changes: 2 additions & 2 deletions src/lib.zig
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ pub const Value = union(enum) {
}

pub fn getU(self: Value, key: []const u8) ?u64 {
return @intCast(u64, self.getI(key) orelse return null);
return @intCast(self.getI(key) orelse return null);
}
};

Expand Down Expand Up @@ -190,7 +190,7 @@ fn PeekableReader(comptime ReaderType: type) type {

const Self = @This();
pub const Error = ReaderType.Error;
pub const Reader = std.io.Reader(*Self, Error, read);
pub const Reader = std.io.GenericReader(*Self, Error, read);

fn read(self: *Self, dest: []u8) Error!usize {
if (self.buf) |c| {
Expand Down
6 changes: 3 additions & 3 deletions src/main.zig
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const std = @import("std");

const bencode = @import("./lib.zig");
const bencode = @import("bencode");

pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const alloc = gpa.allocator();

const file = @embedFile("./../archlinux-2021.04.01-x86_64.iso.torrent");
const file = @embedFile("torrent_file");

var buf = std.io.fixedBufferStream(file);
const r = buf.reader();
const ben = try bencode.parse(r, alloc);
Expand Down

0 comments on commit 8a2e199

Please sign in to comment.