Skip to content

Instantly share code, notes, and snippets.

pub trait FromJson {
fn from_json(j :&Json) -> Result<Self, ValueError>;
}
impl<T: FromJson> FromJson for Option<T> {
fn from_json(j: &Json) -> Result<Option<T>, ValueError> {
match *j {
Null => Ok(None),
otherwise => {
let t = FromJson::from_json(&otherwise);
#[test]
fn test_req() {
let url = match url::from_str("http://www.reddit.com") {
Ok(url) => {
println(url.to_str());
url
}
Err(msg) => fail!("\\{ url: {0}, err: {1} \\}","http://www.reddit.com", msg),
};
trait Plode {
fn explode(&mut self) -> ~str {
~"Boom"
}
fn implode(&mut self) -> ~str {
~"Pop"
}
}
@MarkJr94
MarkJr94 / debugmacro.rs
Created September 17, 2013 03:19
Conditional compilation/execution
fn mutate(x: &mut int) -> int {
let ret = x.clone();
*x += 100;
ret
}
fn main() {
let mut x = -5;
x += 5;
@MarkJr94
MarkJr94 / staticprobs.rs
Last active December 23, 2015 05:39
Static issue???
struct Test {
mem: int,
mem2: int,
}
pub static g_test: Test = Test {
mem: 0,
mem2: 0,
};
fn slice_from_c_str<'a>(cstr: *c_char) -> &'a str {
unsafe {
cast::transmute_region(str::raw::c_str_to_static_slice(cstr))
}
}
config.rs:49 cast::transmute_region(str::raw::c_str_to_static_slice(cstr))
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
config.rs:48:4: 50:5 error: mismatched types: expected `&'a str` but found `&<V1>` (expected &'a str but found &-ptr)
config.rs:48 unsafe {
@MarkJr94
MarkJr94 / rbtreestuff.rs
Last active December 22, 2015 23:09
borrow problems
#[deriving(TotalEq, ToStr, Clone)]
enum Color {
Red,
Black
}
impl Not<Color> for Color {
#[inline]
fn not(&self) -> Color {
match *self {
@MarkJr94
MarkJr94 / life.rs
Created August 6, 2013 20:30
Weird lifetime issue
md_ref = line.split_iter(' ')
.transform(|s| s.to_owned())
.collect::<~[~str]>()[2]
.iter()
.collect::<~[char]>()
.chunk_iter(2)
.transform(|cs| {
let s = str::from_chars(cs);
println(s);
u8::from_str_radix(s, 16).get()
@MarkJr94
MarkJr94 / reference.c
Last active December 20, 2015 16:39
keccak reference.c
/*
The Keccak sponge function, designed by Guido Bertoni, Joan Daemen,
Michaël Peeters and Gilles Van Assche. For more information, feedback or
questions, please refer to our website: http://keccak.noekeon.org/
Implementation by the designers,
hereby denoted as "the implementer".
To the extent possible under law, the implementer has waived all copyright
and related or neighboring rights to the source code in this file.
@MarkJr94
MarkJr94 / keccaktest.c
Created August 6, 2013 10:22
Keccak test
STATUS_CODES
genShortMsg(int hashbitlen)
{
char fn[32], line[SUBMITTER_INFO_LEN];
int msglen, msgbytelen, done;
BitSequence Msg[256], MD[64];
FILE *fp_in, *fp_out;
if ( (fp_in = fopen("ShortMsgKAT.txt", "r")) == NULL ) {
printf("Couldn't open <ShortMsgKAT.txt> for read\n");