This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern crate take_mut; | |
let mut a = 5i32; | |
let mut b = 6i32; | |
take_mut::scope(|scope| { | |
let (a_val, a_hole) = scope.take(&mut a); | |
let (b_val, b_hole) = scope.take(&mut b); | |
b_hole.fill(a_val); | |
a_hole.fill(b_val); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![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 { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![feature(specialization)] | |
struct Foo; | |
struct No; | |
struct Yes; | |
trait Bar { | |
type Result; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![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> { |