Skip to content

Instantly share code, notes, and snippets.

fn modes_(&mut self, reg: u8, modecode: u8) -> AddressingMode {
match (reg, modecode) {
(2,0b00) => Direct,
(2,0b01) => Absolute(self.next_inst()),
(2,0b10) => Const(4),
(2,0b11) => Const(8),
(3,0b00) => Const(0),
(3,0b01) => Const(1),
(3,0b10) => Const(2),
(3,0b11) => Const(-1),
@huonw
huonw / blackmagic.rs
Created January 15, 2014 12:42
do-while loops in Rust
while {
let x = foo();
bar(x);
x != 0
} {}
@huonw
huonw / gist:8366072
Created January 11, 2014 02:06 — forked from brson/gist:8366039
macro_rules! report_diag (
($f: tt, $name: tt, $msg: tt, $(arg: tt)*) => { {
reg_diag_msg!($name, $msg);
let msg = format!($msg, $($arg)*);
let msg = format!("{}: {}", stringify!($name), msg);
$f(msg);
} };
($f: tt, $name: tt, $msg: tt) => { {
reg_diag_msg!($name, $msg);
let msg = format!("{}: {}", stringify!($name), $msg);
@huonw
huonw / gist:7351219
Created November 7, 2013 08:41 — forked from cnd/gist:7351205
fn ifV<'a, T>(rc : &str, star: &'a T) -> Option<&'a T> {
if (Path::new( rc )).exists() {
Some(star)
} else {
None
}
}
@huonw
huonw / build.sh
Created October 11, 2013 23:57
Dynamic function calling
rustc --lib call.rs
rustc --lib other_sym.rs
rustc -L . main.rs
./main
fn count_word_use<'a>(string: &'a str) -> HashMap<&'a str, int> {
let mut word_list = HashMap::new();
for word in string.split_iter(' ') {
word_list.insert_or_update_with(word, 1, |k, v| { *v += 1; });
}
word_list
}
use std::cell::Cell;
use std::str;
use std::rt::io::{Writer, Listener};
use std::rt::io::Reader;
use std::rt::io::net::tcp::TcpListener;
use std::rt::io::net::ip::{SocketAddr, Ipv4Addr};
fn main() {
let port = match std::os::args() {
trait A {}
fn foo<T: A>(_: &T) {}
struct B;
impl A for B {}
impl<'self> A for &'self A {}
fn main() {
let a = B;
@huonw
huonw / gist:6239071
Last active December 21, 2015 02:59 — forked from alexcrichton/gist:6239056
use std::rt::io;
struct HttpResponse<'self> {
out: &'self mut io::Writer,
}
fn write_http_header(_: &mut &mut io::Writer) {}
// fn write_http_header<W: io::Writer>(_: &mut W) {}
impl<'self> HttpResponse<'self> {
enum StrangeCountIterator { priv A(int), priv B(int) }
impl Iterator<int> for StrangeCountIterator {
fn next(&mut self) -> Option<int> {
match *self {
A(mut i) => {
i += 1;
*self = B(i)
return Some(i);
}