Skip to content

Instantly share code, notes, and snippets.

View DutchGhost's full-sized avatar

DutchGhost DutchGhost

View GitHub Profile
@DutchGhost
DutchGhost / unreachable.rs
Created June 11, 2019 18:57
stable unreachable function
#![no_std]
#[inline]
pub const unsafe fn fake_type<T>() -> T {
hint_unreachable()
}
#[inline]
pub const unsafe fn hint_unreachable() -> ! {
fake_type()
}
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 / LICENSE
Last active November 19, 2021 19:30
Const parsing of &str -> usize in Rust
MIT License
Copyright (c) 2021 DutchGhost
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
@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;