Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ikskuh/any-pointer
A type erasure library for Zig that is meant to be eventually upstreamed to std
https://github.com/ikskuh/any-pointer
zig zig-package ziglang
Last synced: about 1 month ago
JSON representation
A type erasure library for Zig that is meant to be eventually upstreamed to std
- Host: GitHub
- URL: https://github.com/ikskuh/any-pointer
- Owner: ikskuh
- License: mit
- Created: 2021-11-05T14:45:56.000Z (about 3 years ago)
- Default Branch: master
- Last Pushed: 2024-08-30T06:23:05.000Z (2 months ago)
- Last Synced: 2024-10-01T03:22:26.247Z (about 1 month ago)
- Topics: zig, zig-package, ziglang
- Language: Zig
- Homepage:
- Size: 25.4 KB
- Stars: 32
- Watchers: 3
- Forks: 2
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# any-pointer type erasure
This package provides a single file `any-pointer.zig` that implements a type-erased pointer for Zig.
This pointer type supports three functions `make`, `cast` and `isNull` and exports the symbol `null_pointer`.
```zig
const AnyPointer = @import("any-pointer").AnyPointer;var i: u32 = 0;
const erased = AnyPointer.make(*u32, &i);
const ptr = erased.cast(*u32);
ptr.* = 42;
std.debug.assert(!ptr.isNull());
std.debug.assert(i == 42);
```In safe modes (`Debug` and `ReleaseSafe`) `cast` will type-check the pointer and might `@panic` when a type confusion would happen.
## Usage
Just add a package pointing to `any-pointer.zig` to your project.
The package will export three types:
- `SafePointer`, which will provide type checking and panics in safe modes.
- `UnsafePointer`, which will not provide type checking and will only have the size of a single pointer.
- `AnyPointer`, which will be `SafePointer` in safe modes and `UnsafePointer` in unsafe modes.In addition to `make`, `cast` and `isNull`, `SafePointer` also has the function `tryCast` which works like `cast`, but will return an optional.