Skip to content

Instantly share code, notes, and snippets.

View Aatch's full-sized avatar

James Miller Aatch

View GitHub Profile
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 / borrow-example.rs
Last active July 5, 2023 04:22 — forked from kolmodin/rust-json.rs
An example and explanation of how to use lifetimes and borrowing to avoid copying, while maintaining safety.
extern mod extra;
use extra::json::*;
/*
* This function manages to do absolutely no copying, which is pretty cool.
*
* "What are all those `'r`s?" you ask. Well, they're liftime parameters. They
* indicate how long something lasts (before it's freed). They can't change how
* long something lives for, they only allow you to tell the compiler stuff it
@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 ?? ()
@Aatch
Aatch / test.ll
Created June 23, 2013 01:54
#[no_drop_flag]
%struct.Foo = type { i64 }
define void @_ZN14__extensions__9meth_29568finalize16_d7d397690bb61803_00E({ i64, %tydesc*, i8*, i8*, i8 }*) #1 {
static_allocas:
br label %1
; <label>:1 ; preds = %static_allocas
br label %return
return: ; preds = %1
#[allow(default_methods)];
pub enum ParseResult<T> {
Err(ParseError),
Ok(T)
}
pub trait GenParser<T,S,A> {
pub fn parse<Tok:TokenStream<T>>(&mut self, &mut ParseState<Tok, S>) -> ParseResult<A>;
@Aatch
Aatch / dump-2.ll
Last active December 18, 2015 21:29
Optimization issues
; Function Attrs: uwtable
define internal fastcc void @_ZN4main16_d7d397690bb61803_00E() #1 {
static_allocas:
%tmp.i = alloca { i64, %tydesc*, {}*, {}*, { i64* } }, align 8
%tmp5 = alloca i64, align 8
%tmp13 = bitcast i64* %tmp5 to i8*
call void @llvm.lifetime.start(i64 8, i8* %tmp13)
store i64 543210, i64* %tmp5, align 8
%0 = bitcast { i64, %tydesc*, {}*, {}*, { i64* } }* %tmp.i to i8*
call void @llvm.lifetime.start(i64 40, i8* %0)