Skip to content

Instantly share code, notes, and snippets.

View matklad's full-sized avatar

Alex Kladov matklad

View GitHub Profile
@matklad
matklad / concurrent-interner.rs
Last active April 3, 2020 22:58
Concurrent Interner for Rust
use std::{
cell::RefCell,
hash::{BuildHasher, Hash, Hasher},
sync::Arc,
};
// hashbrown = "0.7"
use hashbrown::{hash_map::DefaultHashBuilder, HashMap};
// parking_lot = "0.10"
use parking_lot::{Mutex, RwLock};
@matklad
matklad / rustc-ide-proposal.md
Last active August 30, 2019 17:27
rustc-ide-proposal.md

Title: rustc IDE

Estimate: 1 or 2 meeting

Type: technical

Summary

Figure out further steps to make rustc a perfect-for-ides compiler, based on experience with rust-analyzer so far.

@matklad
matklad / vfs.rs
Created August 21, 2019 09:10
VFS API for Rust
use std::path::Path;
/// VFS provides two benefits over using `fs:
///
/// * consistent snapshots: reading the same file twice is guranteed to return
/// the same result, unless you explicitly advance the revision of VFS
/// * automated file watching: if you read a file or a dir, you will be notified when the
/// file changes.
struct Vfs {}
@matklad
matklad / takeWhileInclusive.kt
Created December 24, 2016 21:03
An "inclusive" version of Kotlin's takeWhile
fun <T> Sequence<T>.takeWhileInclusive(pred: (T) -> Boolean): Sequence<T> {
var shouldContinue = true
return takeWhile {
val result = shouldContinue
shouldContinue = pred(it)
result
}
}
extern crate alias;
use std::cell::Cell;
fn pairwise_diffs(xs: &mut [i32]) {
let xs: &[Cell<i32>] = alias::slice(xs);
// Iterating & mutating *without* indices!
for (x, y) in xs.iter().zip(xs[1..].iter()) {
x.set(y.get() - x.get())
}
@matklad
matklad / result_iterator.rs
Last active November 13, 2017 15:48
Extension traits to work with Rust Iterators of Results conveniently
use result_iterator::ResultIterator;
use std::fs::{self, DirEntry};
use std::io;
use std::path::Path;
fn file_names(path: &Path) -> Result<Vec<String>, io::Error> {
let result = fs::read_dir(path)?
.map_then(|entry| {
let is_file = entry.file_type()?.is_file();
trait Counter: 'static {
fn create(data: &str) -> Self where Self: Sized;
}
struct CounterA(i32);
impl Counter for CounterA {
fn create(data: &str) -> Self where Self: Sized {
CounterA(data.parse::<i32>().unwrap())
}
}
@matklad
matklad / c.cpp
Created September 21, 2017 18:37
#include <vector>
#include <cstdint>
#include <iostream>
#include <algorithm>
void perm(std::vector<int32_t> const& xs, std::vector<int32_t> curr) {
if (curr.size() == xs.size()) {
for (int32_t x: curr) {
std::cout << x << ' ';
}
pkgs.oraclejdk8.overrideDerivation (attrs: rec {
name = "oraclejdk-${productVersion}u${patchVersion}";
src = pkgs.fetchurl {
url = "https://bintray.com/jetbrains/intellij-jdk/download_file?file_path=jbsdk8u152b970.2_linux_x64.tar.gz";
sha256 = sha256_x86_64;
};
preUnpack = ''
mkdir src
pushd src
'';
module HM
let rec loop x = loop x
let mk_dno x = loop loop
let assert_same_type a b =
(fun f -> // manually desuget let to avoid let-polymorphism
let _ = f a in
let _ = f b in
mk_dno loop