Skip to content

Instantly share code, notes, and snippets.

// Rust Bitcoin Library
// Written in 2014 by
// Andrew Poelstra <apoelstra@wpsoftware.net>
//
// To the extent possible under law, the author(s) have dedicated all
// copyright and related and neighboring rights to this software to
// the public domain worldwide. This software is distributed without
// any warranty.
//
// You should have received a copy of the CC0 Public Domain Dedication
@apoelstra
apoelstra / gist:dc3e57b5ef5062ff36aa
Created June 24, 2014 21:39
Patricia Trie implementation (insert/delete/lookup)
// Rust Bitcoin Library
// Written in 2014 by
// Andrew Poelstra <apoelstra@wpsoftware.net>
//
// To the extent possible under law, the author(s) have dedicated all
// copyright and related and neighboring rights to this software to
// the public domain worldwide. This software is distributed without
// any warranty.
//
// You should have received a copy of the CC0 Public Domain Dedication
// Rust Bitcoin Library
// Written in 2014 by
// Andrew Poelstra <apoelstra@wpsoftware.net>
//
// To the extent possible under law, the author(s) have dedicated all
// copyright and related and neighboring rights to this software to
// the public domain worldwide. This software is distributed without
// any warranty.
//
// You should have received a copy of the CC0 Public Domain Dedication
@apoelstra
apoelstra / gist:0c5e05a2d57a91be3c0b
Created June 25, 2014 22:22
Stack overflow in Rust's type system
use std::io::{IoResult, InvalidInput, standard_error};
/// Serialization trait
trait Serial {
fn deserialize<I: Iterator<u8>>(iter: I) -> IoResult<Self>;
}
/// Serialization for Option<Serial>
impl<T:Serial> Serial for Option<T> {
@apoelstra
apoelstra / gist:380034a1457f82bb6b9d
Created June 27, 2014 21:22
println side effect?
fn serialize_iter<'a>(&'a self) -> SerializeIter<'a> {
/*
println!("serialize opt {:} as {:}", self.is_some(),
(match self {
&Some(ref dat) => SerializeIter {
data_iter: Some(box [1u8].iter().map(|n| *n) as Box<Iterator<u8>>),
sub_iter_iter: box vec![ dat as &Serializable ].move_iter(),
sub_iter: None,
sub_started: false
},
@apoelstra
apoelstra / gist:07d8f9c3b619b90f548c
Created June 27, 2014 21:23
println side effect?
fn serialize_iter<'a>(&'a self) -> SerializeIter<'a> {
/*
println!("serialize opt {:} as {:}", self.is_some(),
(match self {
&Some(ref dat) => SerializeIter {
data_iter: Some(box [1u8].iter().map(|n| *n) as Box<Iterator<u8>>),
sub_iter_iter: box vec![ dat as &Serializable ].move_iter(),
sub_iter: None,
sub_started: false
},
fn iterator_ref(opt: &Option<bool>) -> Box<Iterator<u8>> {
match opt {
&Some(_) => box [1u8].iter().map(|n| *n) as Box<Iterator<u8>>,
&None => box [0u8].iter().map(|n| *n) as Box<Iterator<u8>>,
}
}
fn main() {
let mut myref = iterator_ref(&Some(true));
@apoelstra
apoelstra / gist:ce17b6528567988c89c3
Created July 7, 2014 17:36
Tree rotation without Rc
use std::ptr::RawPtr;
type NodeRef<T> = Option<Box<Node<T>>>;
type NodeBackRef<T> = *mut Node<T>;
struct Node<T> {
left: NodeRef<T>,
right: NodeRef<T>,
parent: NodeBackRef<T>,
@apoelstra
apoelstra / gist:aec063249270f2bf9949
Created July 8, 2014 15:05
Borrow problem in Rust
/// Add a new UTXO to the set
pub fn add_utxo(&mut self, txo: TxOut, txid: Sha256dHash, vout: uint) -> bool {
let txid = txid.as_bitv();
// Construct node if needed
let node = match self.tree.lookup_mut(&txid) {
Some(node) => node,
None => {
self.tree.insert(&txid, UtxoNode { out: vec![] });
// Note: If this fails, it indicates a bug in our program and/or an OOM
// condition. But maybe we should do something better than task failure
impl <T:Serializable> Serializable for RefCell<T> {
fn serialize(&self) -> Vec<u8> {
self.borrow().serialize()
}
fn serialize_iter<'a>(&'a self) -> SerializeIter<'a> {
self.borrow().serialize_iter()
}
fn deserialize<I: Iterator<u8>>(iter: I) -> IoResult<RefCell<T>> {