Skip to content

Instantly share code, notes, and snippets.

@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
@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 / 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 / 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 }
use std::io::IoResult;
struct SBuf<T> {
br: T,
current: String
}
impl<T: Buffer> SBuf<T> {
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));
@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 ()
}
@krdln
krdln / fork-join.rs
Last active August 29, 2015 14:06
Simple fork-join concurrency in Rust.
#![feature(unboxed_closures, unboxed_closure_sugar)]
use std::rt::thread::Thread;
use std::kinds::marker::InvariantType;
use std::mem::transmute;
fn main() {
let mut arr = [0, 1, 2, 3, 4];
let _ = { // change _ to _bg and it fails to compile!
let mut handle = arr.mut_iter(); // ref|:| ICEd rustc
@krdln
krdln / send.md
Last active August 29, 2015 14:06
Fork-join concurrency and Send bound

In fork-join concurrency scenario (here's an example of what the interface may look like) it may happen that we would want to pass Arc<Mutex<Vec<&mut int>>> to child threads (you can imagine that instead of int there's really big structure that parent thread could access only by reference, so I think it's a pretty reasonable scenario).

So we want to wrap Vec<&mut int> in a Mutex, but that requires Send and our vector doesn't fulfill Send. So to be able to do that, we may just remove Send bound from Mutex::new. We may think that it won't introduce any unsafety, because Send or 'static bound will be checked by spawn or similar methods anyway.

But unfortunately, now we are able to pass Arc<Mutex<Rc>> too, which for sure shouldn't be legal, because of internal, non-thread safe, mutability of Rc.

So my proposed solution is to keep Send bound for Mutex::new, but decouple Send from 'static. Send would only mean "has Sync env

@krdln
krdln / expanded.rs
Last active August 29, 2015 14:11
Macro for entity manager (only main.rs is updated)
fn main() {
{
let em = entity_manager;
em.entities().filter_map(|entity|
Some(entity)
.and_then(|tuple|
entity_manager
.get_component::<X>(&tuple.0)
.map(|obj|tuple.tup_append(obj))
)