Skip to content

Instantly share code, notes, and snippets.

View Aatch's full-sized avatar

James Miller Aatch

View GitHub Profile
@Aatch
Aatch / bad.rs
Last active August 29, 2015 14:11
fn test() {
let x : [u8, ..256];
let val = get_val();
if val == 0 {
x = [1, ..256];
return x;
} else if val == 3 {
x = [3, ..256];
return x;
@Aatch
Aatch / if_cfg.rs
Last active August 29, 2015 14:21
Handy macro for complex conditional compilation
// Feel free to copy and modify this code if it's useful to you.
macro_rules! if_cfg {
($(#[cfg($cfg:meta)] $it:item)+ fallback: $els:item) => (
$(#[cfg($cfg)] $it)+
#[cfg(not(any($($cfg),*)))] $els
)
}
if_cfg {
#[cfg(target_arch="x86")]
@Aatch
Aatch / gist:8cea53541fc10eee41a4
Created May 31, 2015 12:13
Ramp Benchmarks 2015-06-01
test int::test::bench_add_1000_10 ... bench: 268 ns/iter (+/- 4)
test int::test::bench_add_1000_1000 ... bench: 1666 ns/iter (+/- 131)
test int::test::bench_add_100_100 ... bench: 205 ns/iter (+/- 33)
test int::test::bench_add_10_10 ... bench: 51 ns/iter (+/- 41)
test int::test::bench_add_1_1 ... bench: 39 ns/iter (+/- 33)
test int::test::bench_div_1000_1000 ... bench: 2359 ns/iter (+/- 4169)
test int::test::bench_div_10_10 ... bench: 58 ns/iter (+/- 135)
test int::test::bench_div_1_1 ... bench: 62 ns/iter (+/- 26)
test int::test::bench_div_20_2 ... bench: 226 ns/iter (+/- 30)
test int::test::bench_div_250_250 ... bench: 117 ns/iter (+/- 1094)
extern crate smallvec;
use std::collections::VecDeque;
use std::io::Read;
use smallvec::SmallVec;
pub type LexResult<T> = std::result::Result<T, Error>;
/**
macro_rules! handle_element(
($tag:expr, $string:expr, $ctor:ident, [], $type_id:expr) => ( () );
($tag:expr, $string:expr, $ctor:ident, [ $($field:ident),* ], $type_id:expr, ) => (
if eq_slice($tag, $string) {
let _element = ~$ctor {
parent: Element::new($type_id, ($tag).to_str()),
$(
$field: None
),*
};
@Aatch
Aatch / one_shot.rs
Last active December 16, 2015 11:38
struct OneShot<S, T> (S, ~fn (S) -> T);
impl<S, T> OneShot<S,T> {
fn run(~self) -> T {
let (s, f) = match self {
~OneShot(s, f) => (s,f)
};
f(s)
}
@Aatch
Aatch / p_free.rs
Last active December 16, 2015 13:09
Pointer Freeing - Does this work?
struct my_c_struct {
f1:int,
f2:int
}
type MyStruct = my_c_struct;
impl Drop for MyStruct {
fn finalize(&self) {
unsafe {
@Aatch
Aatch / work_queue.rs
Created May 12, 2013 04:31
Chase & Lev work-stealing algorithm in Rust
use core::unstable::intrinsics::{init, atomic_cxchg, atomic_xchg};
/**
* Implementation of the Chase & Lev Work-Stealing deque.
*
* This requires using owned pointers to data in order to ensure that the data is freed at the
* right time and place, it also allows for some of the operations to be atomic, which would not be
* possible if the data was bigger than a pointer.
*
* One key difference from Chase & Lev is that this implementation zeroes out the location in
; fn f() -> ! { f() }
define internal fastcc void @f() #0 {
static_allocas:
tail call fastcc void @f()
unreachable
}
@Aatch
Aatch / backtrace
Created June 20, 2013 04:12
Segfault
#0 0x00007ffff7886ea4 in task::spawn::spawn_raw_oldsched::make_child_wrapper::anon::expr_fn_18909 () from /home/james/projects/rust/compiler/x86_64-unknown-linux-gnu/stage1/bin/../lib/libstd-6c65cf4b443341b1-0.7-pre.so
#1 0x00007ffff790c36c in __morestack () from /home/james/projects/rust/compiler/x86_64-unknown-linux-gnu/stage1/bin/../lib/libstd-6c65cf4b443341b1-0.7-pre.so
#2 0x00007ffff7fc4315 in task_start_wrapper (a=0x7fffec207c80) at /home/james/projects/rust/compiler/src/rt/rust_task.cpp:166
#3 0x0000000000000000 in ?? ()