Skip to content

Instantly share code, notes, and snippets.

View DutchGhost's full-sized avatar

DutchGhost DutchGhost

View GitHub Profile
@DutchGhost
DutchGhost / utils.zig
Last active November 9, 2020 16:45
Zutils
/// 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| {
}
#![no_std]
//#![no_implicit_prelude]
#[cfg(feature = "alloc")]
extern crate alloc;
#[cfg(feature = "std")]
extern crate std;
@DutchGhost
DutchGhost / const_unreachable.rs
Created October 7, 2020 20:06
Constant unreachable function
#[inline(always)]
#[track_caller]
const unsafe fn unreachable() -> ! {
#[derive(Copy, Clone)]
enum Void {}
const NEVER: *const Void = [].as_ptr();
match *NEVER {}
}
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,
@DutchGhost
DutchGhost / transmute.rs
Created September 23, 2020 12:08
transmute!
#![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 August 18, 2020 19:49
the _import_ance of oneself
use {
crate as this,
this as me,
me::self as i
};
@DutchGhost
DutchGhost / parse.zig
Last active May 4, 2020 17:27
parseth
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 15:54
zig build test fails on this
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 March 31, 2020 20:07
ChunksExact reduced
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();
@DutchGhost
DutchGhost / collatz.rs
Created February 21, 2020 21:25
Const collatz using traits!
#![feature(const_trait_bound_opt_out)]
#![feature(const_fn)]
#![feature(const_trait_impl)]
#![feature(const_if_match)]
#![feature(const_loop)]
#![feature(const_mut_refs)]
trait CollatzTransform: Sized {
fn transform(self) -> Option<Self>;
}