Skip to content

Instantly share code, notes, and snippets.

View DutchGhost's full-sized avatar

DutchGhost DutchGhost

View GitHub Profile
use core::{
mem::{self, MaybeUninit},
ptr,
};
/// This struct is a simple wrapper around `[MaybeUninit<T>; N]`.
/// It provides some convenience methods for working with arrays that may have uninitialized items.
pub(crate) struct MaybeArray<T, const N: usize> {
array: [MaybeUninit<T>; N]
}
fn helper<'a, 'b, T: ?Sized>(_: &'a &'b (), v: &'b T) -> &'a T { v }
fn helper_mut<'a, 'b, T: ?Sized>(_: &'a &'b (), v: &'b mut T) -> &'a mut T { v }
/// Use black magic fuckery to turn any `&T` into a `&'static T`.
/// May introduce undefined behavior.
pub fn make_static<'a, T: ?Sized>(input: &'a T) -> &'static T {
let f: fn(_, &'a T) -> &'static T = helper;
f(&&(), input)
}
@DutchGhost
DutchGhost / maybearray_simplified.rs
Created August 3, 2019 10:32
Gist for a question about `first_ptr` and `first_ptr_mut`
pub(crate) struct MaybeArray<T, const N: usize> {
array: [MaybeUninit<T>; N],
}
impl<T, const N: usize> Default for MaybeArray<T, { N }> {
#[inline(always)]
fn default() -> Self {
Self {
array: unsafe { MaybeUninit::<_>::uninit().assume_init() },
}
@DutchGhost
DutchGhost / async_await_proxy.rs
Last active August 8, 2019 19:37
A simple proxy with async/await syntax!
#![feature(async_await)]
extern crate structopt;
use structopt::StructOpt;
use std::{
future::Future as StdFuture,
net::{IpAddr, SocketAddr},
};
@DutchGhost
DutchGhost / maybeuninit.zig
Created August 9, 2019 20:03
MaybeUninit in Zig
pub inline fn MaybeUninit(comptime T: type) type {
return packed union {
value: T,
uninit: void,
const Self = @This();
inline fn init(value: T) Self {
return Self { .value = value };
}
@DutchGhost
DutchGhost / apol.rs
Last active August 15, 2019 15:28
A Pile Of Lines
struct __<'__, ___: '__ = &'__ [()]> {
__: &'__ [__<'__, &'__ [___]>],
___: ___,
}
fn main() {
'__: while break '__
== (|___: __<()>| -> () { ___.___ })(__::<()> {
__: &[][..],
___: (),
@DutchGhost
DutchGhost / hinted.rs
Created October 12, 2019 16:37
Compiler hints
#![no_std]
/// Fakes a value of type `T`.
#[inline]
pub const unsafe fn fake_type<T>() -> T {
cold();
unreachable()
}
/// Tells the compiler this code is unreachable.
@DutchGhost
DutchGhost / rawlines.rs
Created December 16, 2019 22:42
Const lines iterator
#![feature(const_mut_refs)]
#![feature(const_fn)]
#![feature(const_if_match)]
#![feature(const_loop)]
#![feature(const_raw_ptr_deref)]
#![feature(const_fn_union)]
#![feature(const_raw_ptr_to_usize_cast)]
#![feature(const_saturating_int_methods)]
use std::marker::PhantomData;
use std::{
ops::{Generator, GeneratorState},
pin::Pin,
};
/// A wrapper struct around Generators,
/// providing a safe implementation of the [`Iterator`] trait.
pub struct GenIter<G>(Option<G>);
impl<G: Generator + Unpin> GenIter<G> {
@DutchGhost
DutchGhost / simdcollatz.rs
Last active January 2, 2020 13:17
Collatz sequence with simd
mod simd {
use core::arch::x86_64::{
__m128i, _mm_add_epi32, _mm_and_si128, _mm_cmpeq_epi32, _mm_mullo_epi32, _mm_set1_epi32,
_mm_setzero_si128, _mm_sllv_epi32, _mm_srlv_epi32, _mm_sub_epi32, _mm_test_all_ones,
_mm_xor_si128,
};
/// Maps from an array to its corresponding Simd type.
pub unsafe trait SimdArray: Copy {
type Lane: Copy;