Skip to content

Instantly share code, notes, and snippets.

use std::{mem, ptr};
use std::ops::Index;
use std::mem::ManuallyDrop;
#[derive(Default)]
pub struct MinMaxHeap<T> {
heap: Vec<T>,
}
impl<T: PartialOrd> MinMaxHeap<T> {
use std::{ops, cmp, iter, mem, ptr, slice};
use std::marker::PhantomData;
use std::mem::MaybeUninit;
use std::rc::Rc;
/// A copy-on-write `Vec`.
pub struct RcVec<T> {
buf: Rc<[MaybeUninit<T>]>,
len: usize,

Async for loops by composing transformations

Rust has three foundational traits: Try for fallible computation, Iterator for resumable computation, and Future for asynchronous computation. These traits are consumed by language features ?, for, and await, that call trait methods under the hood. Further, programs can (or will be able to, or may be able to) implement these traits with language features try, generators, and async.

Today we can compose Try and Iterator (e.g. impl Iterator<Item = Option<T>>), or Try and Future (e.g. impl Future<Item = Result<T, E>>). We would also like to compose Iterator and Future, as described by @withoutboats here:

Evaluated immediately Evaluated asynchronously
Return once fn() -> T async fn() -> T (Future)
Yield many fn() yield T (Iterator) async fn() yield T (Stream)
@rpjohnst
rpjohnst / lookup.cpp
Created October 1, 2018 17:00
Clang lookup
#include <clang/Frontend/CompilerInstance.h>
#include <clang/Frontend/FrontendActions.h>
#include <clang/Sema/Sema.h>
#include <clang/Sema/Lookup.h>
class ASTInspector : public clang::ASTConsumer {
clang::CompilerInstance &CI;
public:
ASTInspector(clang::CompilerInstance &CI) : CI(CI) {}
@rpjohnst
rpjohnst / cube.rs
Last active March 31, 2018 20:36
Cache behavior of various memory layouts and iteration orders
#![feature(box_syntax, test)]
extern crate test;
#[cfg(test)]
mod tests {
use test::Bencher;
#[bench]
fn single(b: &mut Bencher) {
use std::cell::{Cell, RefCell};
use std::collections::HashMap;
use std::ffi::{CStr, CString};
use std::fs::{OpenOptions, File};
use std::io::{self, Read, Write, BufRead, BufReader, BufWriter};
use std::os::raw::c_char;
thread_local! {
static NEXT_ID: Cell<i32> = Cell::new(0);
static FILES: RefCell<HashMap<i32, BufStream<File>>> = RefCell::new(HashMap::new());
using System;
namespace ValueTypes
{
struct Test
{
public int x { get; set; }
}
class Program
@rpjohnst
rpjohnst / playground.rs
Created May 21, 2017 21:11 — forked from anonymous/playground.rs
Shared via Rust Playground
fn build<V>(n: usize) -> Vec<V> where V: Clone + Default {
vec![V::default(); n]
}
fn compute() -> Vec<Vec<i32>> {
let mut values = build(2);
values[0].push(1);
values
}
#define move_bounce
/// move_bounce(precise, all)
var precise = argument0;
var non_solid = argument1;
if !place_open(non_solid, x, y) {
action_move_to(xprevious, yprevious);
}
@rpjohnst
rpjohnst / print-functions.cc
Created April 30, 2016 22:38
Clang libTooling
#include "clang/AST/AST.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/ASTConsumer.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/Frontend/FrontendActions.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Tooling/CommonOptionsParser.h"
#include "clang/Tooling/Tooling.h"
using namespace std;