This commit is contained in:
Dario48true 2025-06-16 20:43:47 +02:00
parent 796eb066bb
commit 66674cf5db
6 changed files with 50 additions and 47 deletions

View file

@ -26,8 +26,13 @@ pub fn build(b: *std.Build) void {
.root_source_file = b.path("src/root.zig"),
.target = target,
.optimize = optimize,
.link_libc = true,
});
lib_mod.addIncludePath(b.path("src/"));
lib_mod.addLibraryPath(b.path("imports/"));
lib_mod.linkSystemLibrary("uwurandom_c", .{ .needed = true });
// We will also create a module for our other entry point, 'main.zig'.
const exe_mod = b.createModule(.{
// `root_source_file` is the Zig "entry point" of the module. If a module
@ -49,7 +54,7 @@ pub fn build(b: *std.Build) void {
// for actually invoking the compiler.
const lib = b.addLibrary(.{
.linkage = .static,
.name = "zig_binding",
.name = "uwurandom-rs-zig-binding",
.root_module = lib_mod,
});
@ -61,7 +66,7 @@ pub fn build(b: *std.Build) void {
// This creates another `std.Build.Step.Compile`, but this one builds an executable
// rather than a static library.
const exe = b.addExecutable(.{
.name = "zig_binding",
.name = "zig-uwurandom-example",
.root_module = exe_mod,
});

BIN
imports/libuwurandom_c.so Executable file

Binary file not shown.

View file

@ -1,43 +1,15 @@
//! By convention, main.zig is where your main function lives in the case that
//! you are building an executable. If you are making a library, the convention
//! is to delete this file and start with root.zig instead.
pub fn main() !void {
// Prints to stderr (it's a shortcut based on `std.io.getStdErr()`)
std.debug.print("All your {s} are belong to us.\n", .{"codebase"});
var args = try std.process.argsWithAllocator(std.heap.smp_allocator);
_ = args.next();
// stdout is for the actual output of your application, for example if you
// are implementing gzip, then only the compressed bytes should be sent to
// stdout, not any debugging messages.
const stdout_file = std.io.getStdOut().writer();
var bw = std.io.bufferedWriter(stdout_file);
const stdout = bw.writer();
try stdout.print("Run `zig build test` to run the tests.\n", .{});
try bw.flush(); // Don't forget to flush!
}
test "simple test" {
var list = std.ArrayList(i32).init(std.testing.allocator);
defer list.deinit(); // Try commenting this out and see if zig detects the memory leak!
try list.append(42);
try std.testing.expectEqual(@as(i32, 42), list.pop());
}
test "use other module" {
try std.testing.expectEqual(@as(i32, 150), lib.add(100, 50));
}
test "fuzz example" {
const Context = struct {
fn testOne(context: @This(), input: []const u8) anyerror!void {
_ = context;
// Try passing `--fuzz` to `zig build test` and see if it manages to fail this test case!
try std.testing.expect(!std.mem.eql(u8, "canyoufindme", input));
const uwurandom = try lib.generate(blk: {
if (args.next()) |arg| {
break :blk try std.fmt.parseInt(i32, arg, 10);
}
};
try std.testing.fuzz(Context{}, Context.testOne, .{});
break :blk 10;
});
defer uwurandom.deuwu();
std.debug.print("{s}", .{uwurandom.generated});
}
const std = @import("std");

View file

@ -1,13 +1,36 @@
//! By convention, root.zig is the root source file when making a library. If
//! you are making an executable, the convention is to delete this file and
//! start with main.zig instead.
//! zig bindings for uwurandom-rs
//! based on the c bindings for uwurandom-rs, also by me
//! to use this library first generate, this will give you a struct with the "generated" propriety, that is your string
//! when you are done with the string just deuwu
//! ignore _ptr it's for internal use only
const std = @import("std");
const testing = std.testing;
const c_uwurandom = @cImport(@cInclude("uwurandom.h"));
const uwurandom = @This();
pub export fn add(a: i32, b: i32) i32 {
return a + b;
/// the generated string
generated: []u8,
/// INTERNAL USE ONLY DO NOT TOUCH
_ptr: [*c]u8,
/// this return a struct with generated and _ptr filled out
/// please call deuwu or there will be consequences (a memory leak)
pub fn generate(num: i32) !uwurandom {
const ptr: [*:0]u8 = c_uwurandom.uwurandom(num);
std.log.debug("{s}", .{ptr});
return uwurandom{
.generated = try std.fmt.allocPrint(std.heap.c_allocator, "{s}", .{ptr}),
._ptr = ptr,
};
}
test "basic add functionality" {
try testing.expect(add(3, 7) == 10);
/// call the c binding to the rust function to deallocate the string
pub fn deuwu(self: uwurandom) void {
c_uwurandom.deuwu(self._ptr);
}
test "test_it_works" {
const gen = try generate(10);
defer gen.deuwu();
try std.testing.expect(gen.generated.len >= 10);
}

3
src/uwurandom.h Normal file
View file

@ -0,0 +1,3 @@
#pragma once
char* uwurandom(long num);
void deuwu(char*);