Skip to content

Commit 7c74f6a

Browse files
chore!: adjust for Zig 0.16 (#59)
1 parent 1944b2f commit 7c74f6a

11 files changed

Lines changed: 138 additions & 156 deletions

File tree

.github/workflows/cd.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ jobs:
5454
- name: Install Zig
5555
uses: mlugg/setup-zig@v2
5656
with:
57-
version: 0.14.0
57+
version: 0.16.0
5858

5959
- name: Show Zig version
6060
run: |

.github/workflows/ci.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ jobs:
3131
- name: Install Zig
3232
uses: mlugg/setup-zig@v2
3333
with:
34-
version: 0.14.0
34+
version: 0.16.0
3535

3636
- name: Show Zig version
3737
run: |
@@ -54,7 +54,7 @@ jobs:
5454
- name: Install Zig
5555
uses: mlugg/setup-zig@v2
5656
with:
57-
version: 0.14.0
57+
version: 0.16.0
5858

5959
- name: Install kcov
6060
run: |
@@ -86,7 +86,7 @@ jobs:
8686
- name: Install Zig
8787
uses: mlugg/setup-zig@v2
8888
with:
89-
version: 0.14.0
89+
version: 0.16.0
9090

9191
- name: Check formatting
9292
run: zig fmt --check .

.github/workflows/pages.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ jobs:
3030
- name: Install Zig
3131
uses: mlugg/setup-zig@v2
3232
with:
33-
version: 0.14.0
33+
version: 0.16.0
3434

3535
- name: Show Zig version
3636
run: |

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
zig-cache/
88
.zig-cache/
99
zig-out/
10+
zig-pkg/
1011
build/
1112
build-*/
1213
docgen_tmp/

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM eloitor/zig:0.14.0 as builder
1+
FROM eloitor/zig:0.16.0 AS builder
22
RUN apk update
33
RUN apk add --no-cache git
44
WORKDIR /app

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767

6868
#### Prerequisites
6969

70-
- [Zig](https://ziglang.org/download/) (`0.14`)
70+
- [Zig](https://ziglang.org/download/) (`0.16`)
7171

7272
#### Instructions
7373

build.zig.zon

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
.name = .linuxwave,
33
.version = "0.1.5",
44
.fingerprint = 0x217eafdf4732b5f9,
5-
.minimum_zig_version = "0.14.0",
5+
.minimum_zig_version = "0.16.0",
66
.paths = .{
77
"build.zig",
88
"build.zig.zon",
@@ -12,8 +12,8 @@
1212
},
1313
.dependencies = .{
1414
.clap = .{
15-
.url = "https://github.com/Hejsil/zig-clap/archive/refs/tags/0.10.0.tar.gz",
16-
.hash = "clap-0.10.0-oBajB434AQBDh-Ei3YtoKIRxZacVPF1iSwp3IX_ZB8f0",
15+
.url = "https://github.com/Hejsil/zig-clap/archive/refs/tags/0.12.0.tar.gz",
16+
.hash = "clap-0.12.0-oBajB7foAQDqlSwaSG5g0yq7xGbQARUsBk5T64gAOqP5",
1717
},
1818
},
1919
}

src/file.zig

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,34 @@ const std = @import("std");
44

55
/// Reads the given file and returns a byte array with the length of `len`.
66
pub fn readBytes(
7+
io: std.Io,
78
allocator: std.mem.Allocator,
89
path: []const u8,
910
len: usize,
1011
) ![]u8 {
11-
const file = try std.fs.cwd().openFile(path, .{});
12-
defer file.close();
13-
var list = try std.ArrayList(u8).initCapacity(allocator, len);
14-
var buffer = list.allocatedSlice();
15-
const bytes_read = try file.read(buffer);
16-
return buffer[0..bytes_read];
12+
const file = try std.Io.Dir.cwd().openFile(io, path, .{});
13+
defer file.close(io);
14+
var read_buffer: [1024]u8 = undefined;
15+
var file_reader = file.reader(io, &read_buffer);
16+
const reader = &file_reader.interface;
17+
return reader.readAlloc(allocator, len);
1718
}
1819

1920
test "read bytes from the file" {
21+
const allocator = std.testing.allocator;
22+
const io = std.testing.io;
23+
2024
// Get the current directory.
2125
var cwd_buffer: [std.fs.max_path_bytes]u8 = undefined;
22-
const cwd = try std.posix.getcwd(&cwd_buffer);
26+
const cwd_len = try std.process.currentPath(io, &cwd_buffer);
27+
const cwd = cwd_buffer[0..cwd_len];
2328

2429
// Concatenate the current directory with the builder file.
25-
const allocator = std.testing.allocator;
2630
const path = try std.fs.path.join(allocator, &.{ cwd, "build.zig" });
2731
defer allocator.free(path);
2832

2933
// Read the contents of the file and compare.
30-
const bytes = try readBytes(allocator, path, 9);
34+
const bytes = try readBytes(io, allocator, path, 9);
3135
try std.testing.expectEqualStrings("const std", bytes);
3236
defer allocator.free(bytes);
3337
}

src/gen.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub const Generator = struct {
3131
///
3232
/// Returns an array that contains the amplitudes of the sound wave at a given point in time.
3333
pub fn generate(self: Generator, allocator: std.mem.Allocator, sample: u8) ![]u8 {
34-
var buffer = std.ArrayList(u8).init(allocator);
34+
var buffer: std.ArrayList(u8) = .empty;
3535
var i: usize = 0;
3636
while (i < sample_count) : (i += 1) {
3737
// Calculate the frequency according to the equal temperament.
@@ -45,9 +45,9 @@ pub const Generator = struct {
4545
// Apply the volume control.
4646
const volume: f32 = @floatFromInt(self.config.volume);
4747
amp = amp * volume / 100;
48-
try buffer.append(@intFromFloat(amp));
48+
try buffer.append(allocator, @trunc(amp));
4949
}
50-
return buffer.toOwnedSlice();
50+
return buffer.toOwnedSlice(allocator);
5151
}
5252
};
5353

src/main.zig

Lines changed: 53 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,33 +8,37 @@ const build_options = @import("build_options");
88
const clap = @import("clap");
99

1010
/// Runs `linuxwave`.
11-
fn run(allocator: std.mem.Allocator, output: anytype) !void {
11+
fn run(io: std.Io, allocator: std.mem.Allocator, output: *std.Io.Writer, argv: std.process.Args) !void {
1212
// Parse command-line arguments.
13-
const cli = try clap.parse(clap.Help, &args.params, args.parsers, .{ .allocator = allocator });
13+
const cli = try clap.parse(clap.Help, &args.params, args.parsers, argv, .{ .allocator = allocator });
1414
defer cli.deinit();
1515
if (cli.args.help != 0) {
1616
try output.print("{s}\n", .{args.banner});
17-
return clap.help(output, clap.Help, &args.params, args.help_options);
18-
} else if (cli.args.version != 0) {
17+
try clap.help(output, clap.Help, &args.params, args.help_options);
18+
try output.flush();
19+
return;
20+
}
21+
if (cli.args.version != 0) {
1922
try output.print("{s} {s}\n", .{ build_options.exe_name, build_options.version });
23+
try output.flush();
2024
return;
2125
}
2226

2327
// Create encoder configuration.
24-
const encoder_config = wav.EncoderConfig{
28+
const encoder_config: wav.EncoderConfig = .{
2529
.num_channels = if (cli.args.channels) |channels| channels else defaults.channels,
2630
.sample_rate = if (cli.args.rate) |rate| @intFromFloat(rate) else defaults.sample_rate,
2731
.format = if (cli.args.format) |format| format else defaults.format,
2832
};
2933

3034
// Create generator configuration.
3135
const scale = s: {
32-
var scale = std.ArrayList(u8).init(allocator);
36+
var scale: std.ArrayList(u8) = .empty;
3337
var splits = std.mem.splitAny(u8, if (cli.args.scale) |s| s else defaults.scale, ",");
3438
while (splits.next()) |chunk| {
35-
try scale.append(try std.fmt.parseInt(u8, chunk, 0));
39+
try scale.append(allocator, try std.fmt.parseInt(u8, chunk, 0));
3640
}
37-
break :s try scale.toOwnedSlice();
41+
break :s try scale.toOwnedSlice(allocator);
3842
};
3943
defer allocator.free(scale);
4044
const generator_config = gen.GeneratorConfig{
@@ -50,63 +54,76 @@ fn run(allocator: std.mem.Allocator, output: anytype) !void {
5054
const buffer = b: {
5155
if (std.mem.eql(u8, input_file, "-")) {
5256
try output.print("Reading {d} bytes from stdin\n", .{data_len});
53-
var list = try std.ArrayList(u8).initCapacity(allocator, data_len);
54-
const buffer = list.allocatedSlice();
55-
const stdin = std.io.getStdIn().reader();
56-
try stdin.readNoEof(buffer);
57-
break :b buffer;
58-
} else {
59-
try output.print("Reading {d} bytes from {s}\n", .{ data_len, input_file });
60-
break :b try file.readBytes(allocator, input_file, data_len);
57+
try output.flush();
58+
var read_buffer: [1024]u8 = undefined;
59+
var stdin_reader = std.Io.File.stdin().reader(io, &read_buffer);
60+
const stdin = &stdin_reader.interface;
61+
break :b try stdin.readAlloc(allocator, data_len);
6162
}
63+
64+
try output.print("Reading {d} bytes from {s}\n", .{ data_len, input_file });
65+
try output.flush();
66+
break :b try file.readBytes(io, allocator, input_file, data_len);
6267
};
6368
defer allocator.free(buffer);
6469

6570
// Generate music.
66-
const generator = gen.Generator.init(generator_config);
67-
var data = std.ArrayList(u8).init(allocator);
71+
const generator: gen.Generator = .init(generator_config);
72+
var data: std.ArrayList(u8) = .empty;
6873
for (buffer) |v| {
6974
const gen_data = try generator.generate(allocator, v);
7075
defer allocator.free(gen_data);
71-
try data.appendSlice(gen_data);
76+
try data.appendSlice(allocator, gen_data);
7277
}
7378

7479
// Encode WAV.
7580
const out = if (cli.args.output) |out| out else defaults.output;
76-
const writer = w: {
81+
var write_buffer: [1024]u8 = undefined;
82+
var fhandle: ?std.Io.File = null;
83+
defer if (fhandle) |f| {
84+
f.close(io);
85+
};
86+
var file_writer = w: {
7787
if (std.mem.eql(u8, out, "-")) {
7888
try output.print("Writing to stdout\n", .{});
79-
break :w std.io.getStdOut().writer();
80-
} else {
81-
try output.print("Saving to {s}\n", .{out});
82-
const out_file = try std.fs.cwd().createFile(out, .{});
83-
break :w out_file.writer();
89+
try output.flush();
90+
break :w std.Io.File.stdout().writer(io, &write_buffer);
8491
}
92+
93+
try output.print("Saving to {s}\n", .{out});
94+
try output.flush();
95+
fhandle = try std.Io.Dir.cwd().createFile(io, out, .{});
96+
break :w fhandle.?.writer(io, &write_buffer);
8597
};
86-
const wav_data = try data.toOwnedSlice();
98+
const wav_data = try data.toOwnedSlice(allocator);
8799
defer allocator.free(wav_data);
88-
try wav.Encoder(@TypeOf(writer)).encode(writer, wav_data, encoder_config);
100+
try wav.encode(&file_writer.interface, wav_data, encoder_config);
89101
}
90102

91103
/// Entry-point.
92-
pub fn main() !void {
93-
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
94-
const allocator = gpa.allocator();
95-
const stderr = std.io.getStdErr().writer();
96-
run(allocator, stderr) catch |err| {
104+
pub fn main(init: std.process.Init) !void {
105+
const allocator = init.gpa;
106+
const io = init.io;
107+
var stderr_buffer: [1024]u8 = undefined;
108+
var stderr_writer = std.Io.File.stderr().writer(io, &stderr_buffer);
109+
const stderr = &stderr_writer.interface;
110+
run(io, allocator, stderr, init.minimal.args) catch |err| {
97111
try stderr.print("Error occurred: {}\n", .{err});
112+
try stderr.flush();
98113
};
99114
}
100115

101116
test "run" {
102117
const allocator = std.testing.allocator;
103-
var buffer = std.ArrayList(u8).init(allocator);
104-
const output = buffer.writer();
105-
run(allocator, output) catch |err| {
118+
const io = std.testing.io;
119+
120+
const fake_args: std.process.Args = .{ .vector = &[_][*:0]const u8{} };
121+
var allocating: std.Io.Writer.Allocating = .init(allocator);
122+
run(io, allocator, &allocating.writer, fake_args) catch |err| {
106123
std.debug.print("Error occurred: {s}\n", .{@errorName(err)});
107124
return;
108125
};
109-
const result = buffer.toOwnedSlice() catch |err| {
126+
const result = allocating.toOwnedSlice() catch |err| {
110127
std.debug.print("Error occurred: {s}\n", .{@errorName(err)});
111128
return;
112129
};

0 commit comments

Comments
 (0)