Skip to content

Instantly share code, notes, and snippets.

// cf. https://doc.rust-lang.org/reference/expressions/method-call-expr.html
trait Or {
fn f(self);
}
struct T;
impl Or for &T {
fn f(self) {
#[derive(Debug)]
struct Holder<T: PrimitiveType>(T::Native);
#[derive(Debug)]
struct Float64 {}
#[derive(Debug)]
struct Int64 {}
#[derive(Debug)]
struct Int32 {}
// [package]
// name = "async-internal-spawn"
// version = "0.1.0"
// authors = ["Ryan James Spencer <spencer.ryanjames@gmail.com>"]
// edition = "2018"
// [dependencies.async-std]
// version = '1.6.2'
// features = ['unstable']
@justanotherdot
justanotherdot / minimal-rustc-nightly-install
Created August 25, 2020 03:40
Minimal nightly install via rustup.
#!/bin/sh -eu
export CARGO_HOME="/usr/local/cargo"
export RUSTUP_HOME="/usr/local/rustup"
export PATH="$CARGO_HOME/bin:$PATH"
curl https://sh.rustup.rs -sSf | sh -s -- --default-toolchain none -y && rustup toolchain install nightly --profile minimal
#[derive(Debug)]
struct X(pub i32);
impl AsRef<X> for X {
fn as_ref(&self) -> &X {
self
}
}
fn f<A: AsRef<X> + std::fmt::Debug>(x: A) {
fn main() {
let x = String::from("foo");
// this does not work because it uses the special `'static` lifetime.
// which means _the entire program_. The main thread could finish and this
// thread could still be running! In that case the reference to x would be invalid.
// we can make this work by passing `move` in front of the closure,
// effectively copying `x` into the closure's environment.
// let normal = std::thread::spawn(|| dbg!(&x));
use std::{
fs::File,
io::{BufReader, Cursor, Result},
path::Path,
};
trait Open<T> {
fn open(&self) -> Result<T>;
}
fn sine_wave_oscillator (w : f32) -> impl Iterator<Item=f32> {
let mut x0 = w.sin();
let mut x1 = 0.0;
let b = 2.0 * w.cos();
std::iter::repeat_with(move || {
let xn = b * x0 - x1;
x1 = x0;
x0 = xn;
xn
/*
If you look, I've also included a pattern to pass the number of times you want
the benchmark to run. I default to ten runs instead of one-hundred here as code
under inspection may take a long time to run under one-hundred times. You will
notice that I'm using nanoseconds for everything, which lets me compute a
proper mean average without rounding.
*/
macro_rules! bench {
($val:expr) => {
{
@justanotherdot
justanotherdot / snapshots.rs
Last active August 4, 2020 23:32
Utilizing closures as snapshots for single or multiple values.
fn main() {
let mut xs = vec![1];
xs[0] = 42;
let ys = xs.clone();
let snapshot = move || ys;
xs[0] = 100;
let reset = snapshot();
assert_eq!(&reset, &vec![42]);
assert_eq!(xs, vec![100]);
xs = reset;