Skip to content

Instantly share code, notes, and snippets.

@krdln
krdln / fat.rs
Last active August 29, 2015 14:06
Slim pointers to fat objects using DST
#![feature(unsafe_destructor, macro_rules)]
#[macro_escape] mod fat {
use std::mem::transmute;
use std::raw::TraitObject;
#[unsafe_no_drop_flag]
pub struct SlimPtr<Sized? T> {
ptr: *mut *mut ()
}
use std::util::replace;
enum List<T> {
Cons(T, ~List<T>),
Nil
}
impl<T> List<T> {
fn prepend(&mut self, elem: T) {
*self = Cons(elem, ~replace(self, Nil));
use std::io::IoResult;
struct SBuf<T> {
br: T,
current: String
}
impl<T: Buffer> SBuf<T> {
@krdln
krdln / doctest.rs
Created June 16, 2014 18:30
How to make `rustdoc --test` work?
#![crate_type="lib"]
/// increments
///
/// ```
/// let x = bar(41);
/// assert_eq!(x, 42);
/// ```
pub fn bar(x: int) -> int { x + 1 }
@krdln
krdln / counter.rs
Created June 10, 2014 23:54
Zliczanie wystąpień słów (Rust master @ 2014-06-10)
use std::collections::HashMap;
use std::path::Path;
use std::io::fs::File;
use std::io::BufferedReader;
fn main() {
let path = Path::new("potop2.txt");
let reader = File::open(&path).unwrap();
let mut reader = BufferedReader::new(reader);
@krdln
krdln / len.rs
Last active August 29, 2015 14:01
trait Iterator<T> {
//...
fn len(self) { // not &mut self !
match self.size_hint() {
(bot, Some(up)) if bot == up => bot,
_ => self.fold(0, |cnt, _x| cnt + 1)
}
}
}
@krdln
krdln / rust.nanorc
Last active August 29, 2015 14:01
Rust nano highlighting
## Simple syntax highlighter for rust
##
syntax "rust" "\.rs$"
## keywords
color brightgreen "\<(as|be|break|copy|else|enum|extern|false|fn|for|if|impl|let|loop|match|mod|mut|priv|pub|ref|return|static|struct|super|true|trait|type|unsafe|use|while|yield|in|crate|continue|box)\>"
color brightwhite "\<(self)\>"
color cyan "\<(mut)\>"
## basic types
enum List_ {
Cons(int, ~List_),
Nil
}
struct List(~List_);
impl List {
fn prepend(&mut self, x : int) {
let List(old) = self; // I have no idea, how to write this line
enum List {
Cons(int, ~List),
Nil
}
impl List {
fn prepend(~self, x : int) -> ~List {
~Cons(x, self)
}
}
@krdln
krdln / monitor.cc
Created October 30, 2013 15:36
Wrapping objects behind mutex. (monitor to zła nazwa (haha, mieszamy języki))
#include <cstdio>
#include <thread>
#include <future>
#include <mutex>
using namespace std;
struct Obiekt {
int x;
void f() { printf("Hej %d!\n", x); }
};