Skip to content

Commit a931766

Browse files
feat: initial code
1 parent bcaa7c4 commit a931766

File tree

5 files changed

+110
-1
lines changed

5 files changed

+110
-1
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
zig-out
2+
zig-cache
3+
result
4+
result-*

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,9 @@
1-
# any-plus
1+
# Any+
2+
23
Any+ is a Zig library for handling anytypes in the runtime.
4+
5+
## Features
6+
7+
- [x] Runtime `anytype`
8+
- [ ] Any-reader
9+
- [ ] Any-writer

any+.zig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/// Anytype in runtime
2+
pub const Anytype = @import("any+/anytype.zig");

any+/anytype.zig

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
const std = @import("std");
2+
const Self = @This();
3+
4+
type: []const u8,
5+
size: usize = 0,
6+
ptr: *anyopaque,
7+
8+
pub inline fn init(value: anytype) Self {
9+
return initExplicit(@TypeOf(value), value);
10+
}
11+
12+
pub inline fn initExplicit(comptime T: type, value: T) Self {
13+
var size: usize = @sizeOf(T);
14+
const ptr: *anyopaque = switch (@typeInfo(T)) {
15+
.Int, .ComptimeInt => @ptrFromInt(value),
16+
.Float, .ComptimeFloat => @ptrFromInt(@as(usize, @bitCast(@as(f128, @floatCast(value))))),
17+
.Enum => @ptrFromInt(@intFromEnum(value)),
18+
.Struct, .Union => @constCast(&value),
19+
.Pointer => |p| switch (@typeInfo(p.child)) {
20+
.Array => blk: {
21+
size = value.len * @sizeOf(p.child);
22+
break :blk @ptrCast(@constCast(value.ptr));
23+
},
24+
else => |f| @compileError("Unsupported pointer type: " ++ @tagName(f)),
25+
},
26+
else => |f| @compileError("Unsupported type: " ++ @tagName(f)),
27+
};
28+
29+
return .{
30+
.type = @typeName(T),
31+
.size = size,
32+
.ptr = ptr,
33+
};
34+
}
35+
36+
pub inline fn cast(self: Self, comptime T: type) error{InvalidCast}!T {
37+
if (!std.mem.eql(u8, self.type, @typeName(T))) return error.InvalidCast;
38+
39+
return switch (@typeInfo(T)) {
40+
.Int, .ComptimeInt => @intFromPtr(self.ptr),
41+
.Float, .ComptimeFloat => @floatCast(@as(f128, @bitCast(self.ptr))),
42+
.Enum => |e| @enumFromInt(@as(e.tag_type, @ptrFromInt(self.ptr))),
43+
.Struct, .Union => @ptrCast(@alignCast(self.ptr)),
44+
.Pointer => |p| switch (@typeInfo(p.child)) {
45+
.Array => blk: {
46+
const length = @divExact(self.size, @sizeOf(p.child));
47+
break :blk @as([*]p.child, @ptrCast(@alignCast(self.ptr)))[0..length];
48+
},
49+
else => |f| @compileError("Unsupported pointer type: " ++ @tagName(f)),
50+
},
51+
else => |f| @compileError("Unsupported type: " ++ @tagName(f)),
52+
};
53+
}
54+
55+
pub inline fn len(self: Self, comptime T: type) usize {
56+
const size = switch (@typeInfo(T)) {
57+
.Pointer => |p| @sizeOf(p.child),
58+
else => @sizeOf(T),
59+
};
60+
61+
return @divExact(self.size, size);
62+
}

build.zig

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const std = @import("std");
2+
3+
pub fn build(b: *std.Build) void {
4+
const target = b.standardTargetOptions(.{});
5+
const optimize = b.standardOptimizeOption(.{});
6+
const no_docs = b.option(bool, "no-docs", "skip installing documentation") orelse false;
7+
8+
_ = b.addModule("any+", .{
9+
.source_file = .{ .path = b.pathFromRoot("any+.zig") },
10+
});
11+
12+
const step_test = b.step("test", "Run all unit tests");
13+
14+
const unit_tests = b.addTest(.{
15+
.root_source_file = .{
16+
.path = b.pathFromRoot("any+.zig"),
17+
},
18+
.target = target,
19+
.optimize = optimize,
20+
});
21+
22+
const run_unit_tests = b.addRunArtifact(unit_tests);
23+
step_test.dependOn(&run_unit_tests.step);
24+
25+
if (!no_docs) {
26+
const docs = b.addInstallDirectory(.{
27+
.source_dir = unit_tests.getEmittedDocs(),
28+
.install_dir = .prefix,
29+
.install_subdir = "docs",
30+
});
31+
32+
b.getInstallStep().dependOn(&docs.step);
33+
}
34+
}

0 commit comments

Comments
 (0)