Skip to content

Instantly share code, notes, and snippets.

Avatar

DutchGhost DutchGhost

View GitHub Profile
@DutchGhost
DutchGhost / usingnamespace.zig
Created Nov 18, 2020
funny usingnamespcae weirdness
View usingnamespace.zig
usingnamespace closure: {
var hashes: []const usize = &[0]usize{};
break :closure struct {
pub fn ctStrHash(comptime str: []const u8) usize {
const hash = yourAlgo(str);
comptime {
for (hashes) |existing| {
if (existing == hash) @compileError("collision");
}
View utils.zig
/// This piece of code is used whenever the number of iterations is comptime known.
/// `n` == iterations.
for ([_]u8{0} ** n) |_, i| {
}
/// This piece of code is used whenever the number of iterations is runtime known.
//// `n` == iterations.
for (@as([*]u0, undefined)[0..n]) |_, i| {
}
View template.rs
#![no_std]
//#![no_implicit_prelude]
#[cfg(feature = "alloc")]
extern crate alloc;
#[cfg(feature = "std")]
extern crate std;
@DutchGhost
DutchGhost / const_unreachable.rs
Created Oct 7, 2020
Constant unreachable function
View const_unreachable.rs
#[inline(always)]
#[track_caller]
const unsafe fn unreachable() -> ! {
#[derive(Copy, Clone)]
enum Void {}
const NEVER: *const Void = [].as_ptr();
match *NEVER {}
}
View build.zig
const Builder = @import("std").build.Builder;
const std = @import("std");
const cpu = @import("std").Target.arm;
pub fn build(b: *Builder) void {
const mode = b.standardReleaseOptions();
const lib = b.addStaticLibrary("krate", "src/main.zig");
lib.setBuildMode(mode);
lib.target = std.zig.CrossTarget{
.cpu_arch = .thumb,
.os_tag = .freestanding,
View transmute.rs
#![feature(untagged_unions)]
#![feature(const_generics, const_evaluatable_checked)]
#![allow(incomplete_features)]
use core::mem::ManuallyDrop;
struct Bool<const B: bool>;
trait True {}
@DutchGhost
DutchGhost / poem.rs
Created Aug 18, 2020
the _import_ance of oneself
View poem.rs
use {
crate as this,
this as me,
me::self as i
};
View parse.zig
const std = @import("std");
const testing = std.testing;
const meta = std.meta;
pub const Type = union(enum) {
Character,
Number,
String,
};
@DutchGhost
DutchGhost / testfail.zig
Last active May 1, 2020
zig build test fails on this
View testfail.zig
const std = @import("std");
const testing = std.testing;
const meta = std.meta;
pub const Type = union(enum) {
Character,
};
pub const Ammount = union(enum) {
Infinite,
@DutchGhost
DutchGhost / chunk.rs
Created Mar 31, 2020
ChunksExact reduced
View chunk.rs
use std::marker::PhantomData;
use std::slice;
pub fn chunks_exact<T>(slice: &[T], chunk_size: usize) -> ChunksExact<'_, T> {
assert!(chunk_size != 0);
let rem = slice.len() % chunk_size;
let len = slice.len() - rem;
let (fst, snd) = slice.split_at(len);
let len = fst.len();
You can’t perform that action at this time.