Skip to content

Instantly share code, notes, and snippets.

#![feature(specialization)]
#![allow(dead_code)]
struct Nil;
struct Cons<H, T>(H, T);
struct Zero;
struct Succ<T>(T);
trait At<L> {
#![feature(specialization)]
#![allow(dead_code)]
struct Here;
struct There<T>(T);
struct Nil;
struct Cons<Head, Index, Tail>(Head, Index, Tail);
trait ContainsAt<T, I> {
#![feature(specialization)]
struct Foo;
struct No;
struct Yes;
trait Bar {
type Result;
}
# Demonstration that any Elixir "lens" (function compatible with get_in and get_and_update_in) is equivalent to a lens represented in get/set style
# Modulo the fact that an Elixir lens cannot be written if there is no valid "get" operation
defmodule Optics do
defp id, do: fn x -> x end
defp old_and_new(new), do: fn old -> {old, new} end
def decompose(lens) do
[get: fn(obj) -> lens.(:get, obj, id) end,
set: fn(obj, new) -> lens.(:get_and_update, obj, old_and_new(new)) |> elem(1) end]
trait Streamer<'a> {
type Item;
fn next(&'a mut self) -> Option<Self::Item>;
}
struct SelfStreaming;
impl<'a> Streamer<'a> for SelfStreaming {
type Item = &'a mut Self;
fn next(&'a mut self) -> Option<&'a mut Self> {
trait ApplyLTable<'to> {
type Applied;
}
impl<'from, 'to, T: 'from+'to> ApplyLTable<'to> for &'from T {
type Applied = &'to T;
}
impl<'from, 'to, T: 'from+'to> ApplyLTable<'to> for &'from mut T {
type Applied = &'to mut T;
#![allow(dead_code)]
use std::marker::PhantomData;
use std::sync::atomic::{Ordering, AtomicUsize, ATOMIC_USIZE_INIT};
static mut STORED: [usize; 256] = [0; 256];
static NEXT: AtomicUsize = ATOMIC_USIZE_INIT;
fn store<T>(t: T) -> u8 {
trait Identity {
fn call<T>(&self, T) -> T;
}
macro_rules! id {
($closure:expr) => {{
struct ThisID;
impl Identity for ThisID {
fn call<T>(&self, t: T) -> T {
$closure(t)
@Sgeo
Sgeo / gadts.rs
Last active September 10, 2024 19:24 — forked from mstewartgallus/gadts.rs
Gadts in Rust
/// This type is only every inhabited when S is nominally equivalent to T
#[derive(Debug)]
pub struct Is<S, T>(::std::marker::PhantomData<(*const S, *const T)>);
// Construct a proof of the fact that a type is nominally equivalent
// to itself.
pub fn is<T>() -> Is<T, T> { Is(::std::marker::PhantomData) }
// std::mem::transmute does not accept unsubstituted type parameters
// manual transmute as suggested by manual
trait HKT<U> {
type Base;
type Reapplied;
}
type BaseOf<T> = <T as HKT<()>>::Base;
type Reapply<T, U> = <T as HKT<U>>::Reapplied;
impl<'a, T, U: 'a> HKT<U> for &'a T {
type Base = T;