Skip to content

Instantly share code, notes, and snippets.

impl SetupRequest: xio::IOable {
static fn read<E: xio::Endian Copy>(_e: E, _buf: &_buf/[u8]) ->
(SetupRequest, &_buf/[u8]) {
{
let (byte_order, _buf) = xio::IOable::read(_e, _buf);
{
let _buf = vec::view(_buf, 1, _buf.len());
{
let (protocol_major_version, _buf) =
xio::IOable::read(_e, _buf);
@jld
jld / gist:4748798
Created February 10, 2013 07:37
Alignment is hard.
enum NewType<T> = T;
struct UnAligned<T> { a: u8, b: T }
fn main() {
unsafe {
let p: *UnAligned<u32> = ptr::null();
io::println(fmt!("%?", ptr::addr_of(&(*p).b) as uint));
}
unsafe {
let p: *UnAligned<NewType<u32>> = ptr::null();
@jld
jld / gist:4748858
Last active December 12, 2015 09:08
Alignment: it gets worse.
enum NewType<T> = T;
struct AfterByte<T> { a: u8, b: T }
const C0: AfterByte<u32> = AfterByte { a: 0xA5, b: 0xDEADBEEF };
const C1: AfterByte<NewType<u32>> = AfterByte { a: 0xA5, b: NewType(0xDEADBEEF) };
fn main() {
io::println(fmt!("C0.b = %x", C0.b as uint));
io::println(fmt!("C1.b = %x", *(C1.b) as uint));
}
enum Option<T> { Some(T), None }
fn gen_func<A>(...) {
let o: Option<uint> = ...;
match o { ... }
}
@jld
jld / gist:5020875
Created February 23, 2013 18:56
In which rustc fails an LLVM assertion.
pub enum E {
H = 0x7FFF_FFFF_FFFF_FFFF,
L = 0x8000_0000_0000_0000
}
pub fn f(e: E) -> bool {
match e {
H => true,
L => false
}
@jld
jld / gist:5021325
Last active December 14, 2015 03:29
Rust has a bad day with side-effects.
struct S { x: int, y: int }
fn effect(s: &str) -> S { io::println(s); S { x: 0, y: 0 } }
pub fn main() {
let _foo = S { x: 1, .. effect("this will be printed") };
let _ = S { x: 2, .. effect("and this won't") };
let _ = S { x: effect("but this will").y, .. effect("(still no)") };
}
@jld
jld / gist:5022917
Created February 24, 2013 06:49
Test case for a compiler bug I introduced locally. Order of evaluation is important in affine-land.
struct S { f0: ~str, f1: int }
pub fn main() {
let s = ~"Hello, world!";
let _s = S { f0: str::from_slice(s), ..S { f0: s, f1: 23 } };
io::println(_s.f0);
}
@jld
jld / gist:5022947
Created February 24, 2013 06:58
…and there's more where that came from.
struct S { f0: ~str, f1: ~str }
fn main() {
let s = ~"Hello, world!";
let _s = S { f1: str::from_slice(s), f0: s };
io::println(_s.f0);
io::println(_s.f1);
}
@jld
jld / gist:5068511
Created March 1, 2013 22:39
Rust continues to have a bad day with side effects.
struct S { x: int, y: int }
fn main() {
let w;
let s = S { x: 1, y: 2 };
let _ = S { x: 3, ..({ w = "World"; s }) };
io::println(fmt!("Hello, %s!", w));
}
@jld
jld / gist:5101869
Created March 6, 2013 18:41
357686312646216567629137
#!/bin/sh
thing() {
local stem=$1 found=false
for d in 1 2 3 4 5 6 7 8 9
do local p=$d$stem
if [ $(factor $p | wc -w) -ne 2 ]
then continue
fi
found=true
thing $p || echo $p