Skip to content

Instantly share code, notes, and snippets.

@mikeyhew
mikeyhew / json_str.rs
Last active September 22, 2019 16:53
JsonStr, a Rust unsized string slice type that can include Json escape sequences
// Author: Michael Hewson
// https://gist.github.com/mikeyhew/334122cd0104ad3509388074be4351ba
// released under the Unlicense <https://unlicense.org/>
use std::{
borrow::Cow,
str,
char,
iter,
};
@mikeyhew
mikeyhew / binder.rs
Created January 7, 2019 00:55
A potential new API for ty::Binder
/// A number of lifetime and type parameters that a type is bound with
#[derive(Copy, Clone, Debug)]
pub struct Binder {
num_lifetimes: u32,
num_types: u32,
}
impl Binder {
pub fn bind<T>(self, value: T) -> Bound<T> {
Bound {
@mikeyhew
mikeyhew / playground.rs
Created August 2, 2018 20:45
Any<'a> demo
#![feature(arbitrary_self_types)]
use std::ptr;
type TypeId = String;
trait Any<'a>: 'a {
fn type_id(self: *const Self) -> TypeId;
}
@mikeyhew
mikeyhew / deref_into.rs
Last active June 5, 2018 06:20
DerefMove and DerefInto
#![feature(arbitrary_self_types, specialization)]
use std::{
mem::{self, ManuallyDrop},
ptr::{self, NonNull},
marker::PhantomData,
ops::{Deref, DerefMut},
};
struct Move<'a, T: 'a + ?Sized> {
@mikeyhew
mikeyhew / playground.rs
Created November 18, 2017 02:08 — forked from anonymous/playground.rs
Rust code shared from the playground
extern crate crossbeam;
extern crate rand;
use rand::Rng;
#[allow(unused)]
use std::sync::{RwLock, Mutex};
const ARRAY_LEN: usize = 1000;
const READS_PER_WRITE: u32 = 5;
@mikeyhew
mikeyhew / loop_args.rs
Created November 15, 2017 21:34
Loop parameters/arguments in Rust (potential language feature)
let gcd_42_55 = loop(a = 42, b = 45) {
if y == 0 {
break x
} else {
continue(b, a % b)
}
};
DEBUG:rustc_typeck::collect: convert: item with id 2
DEBUG:rustc_typeck::collect: convert: item std with id 3
DEBUG:rustc_typeck::collect: convert: item Foo with id 4
DEBUG:rustc_typeck::astconv: ast_ty_to_ty(id=NodeId(9), ast_ty=type(&'a isize))
DEBUG:rustc_typeck::astconv: ast_region_to_region(lifetime=lifetime(10: 'a)) yields ReEarlyBound(0, 'a)
DEBUG:rustc_typeck::astconv: TyRef r=ReEarlyBound(0, 'a)
DEBUG:rustc_typeck::astconv: ast_ty_to_ty(id=NodeId(11), ast_ty=type(isize))
DEBUG:rustc_typeck::astconv: ast_ty_to_ty: maybe_qself=None path=path(isize)
DEBUG:rustc_typeck::astconv: base_def_to_ty(def=PrimTy(TyInt(isize)), opt_self_ty=None, path_segments=[PathSegment { name: isize(93), parameters: None, infer_types: false }])
DEBUG:rustc_typeck::astconv: ast_ty_to_ty(id=NodeId(13), ast_ty=type(&'b isize))
@mikeyhew
mikeyhew / Own_trait_and_move_references.rs
Last active November 6, 2017 19:09
`&move` references and an `Own` trait for rust
#![feature(arbitrary_self_types, unboxed_closures, fn_traits)]
use std::mem::{self, ManuallyDrop};
use std::ptr;
use std::slice;
macro_rules! mk_move {
(let $name:ident = &move $expr:expr;) => {
mk_move!(let $name: _ = &move $expr;);
};
use std::fmt;
pub trait Unwrap {
type Target;
type ErrorMessage: fmt::Display;
/// takes Self and either unwraps it into Target,
/// or returns an error message to panic with
fn unwrap(self) -> Result<Self::Target, Self::ErrorMessage>;
@mikeyhew
mikeyhew / thread_local_scratchpad.rs
Last active June 9, 2017 06:50
An example of storing a Vec in a thread-local "scratchpad" static variable, to keep from repeatedly allocating and deallocating eat
use std::cell::UnsafeCell;
#[repr(C)]
struct Channel;
extern {
fn foo(inputs: *mut *mut Channel);
}
fn call_foo(inputs: &mut [&mut [Channel]]) {