Skip to content

Instantly share code, notes, and snippets.

@krdln
krdln / suffixes
Created October 16, 2013 17:42
Example of custom literal suffixes in c++11
#include <cstdio>
struct _Ms { int x; };
struct _Us { int x; };
constexpr _Ms operator "" ms(long long unsigned x) { return {(int)x}; }
constexpr _Us operator"" us(long long unsigned x) { return {(int)x}; }
void fun(_Ms x) { printf("%d milisekund\n", x.x); }
void fun(_Us x) { printf("%d mikrosekund\n", x.x); }
@krdln
krdln / k6.cc
Last active December 25, 2015 17:29
Blinking K6: example of AVR C++IO library.
#define F_CPU 12000000UL
#include <krdln/port.h>
int main() {
Output<A::Pin<0>> led;
Output<A::Pin<2>, false> err; // false: "on" level is 0V
Button<C::Pin<3>> but; // automagic pullup, yay!
int const T = 300;
@krdln
krdln / stdout.rs
Created October 29, 2013 23:55
stdout() and print
use std::rt::io::{stdout,Writer};
fn main() {
print("Hello");
stdout().write(" World".as_bytes());
println("!");
}
use std::rt::io::Writer;
use std::rt::io::stdio::with_task_stdout;
struct MagicWriter();
impl Writer for MagicWriter {
fn write(&mut self, buf: &[u8]) {
do with_task_stdout |o| { o.write(buf); }
}
fn flush(&mut self) {
do with_task_stdout |o| { o.flush(); }
@krdln
krdln / sstream.cc
Last active December 26, 2015 23:49
stringstream
#include <sstream>
#include <iostream>
#include <string>
using namespace std;
string ss2s(ostream const& os) {
return static_cast<stringstream const&>(os).str();
}
int main() {
@krdln
krdln / monitor.cc
Created October 30, 2013 15:36
Wrapping objects behind mutex. (monitor to zła nazwa (haha, mieszamy języki))
#include <cstdio>
#include <thread>
#include <future>
#include <mutex>
using namespace std;
struct Obiekt {
int x;
void f() { printf("Hej %d!\n", x); }
};
enum List {
Cons(int, ~List),
Nil
}
impl List {
fn prepend(~self, x : int) -> ~List {
~Cons(x, self)
}
}
enum List_ {
Cons(int, ~List_),
Nil
}
struct List(~List_);
impl List {
fn prepend(&mut self, x : int) {
let List(old) = self; // I have no idea, how to write this line
@krdln
krdln / rust.nanorc
Last active August 29, 2015 14:01
Rust nano highlighting
## Simple syntax highlighter for rust
##
syntax "rust" "\.rs$"
## keywords
color brightgreen "\<(as|be|break|copy|else|enum|extern|false|fn|for|if|impl|let|loop|match|mod|mut|priv|pub|ref|return|static|struct|super|true|trait|type|unsafe|use|while|yield|in|crate|continue|box)\>"
color brightwhite "\<(self)\>"
color cyan "\<(mut)\>"
## basic types
@krdln
krdln / len.rs
Last active August 29, 2015 14:01
trait Iterator<T> {
//...
fn len(self) { // not &mut self !
match self.size_hint() {
(bot, Some(up)) if bot == up => bot,
_ => self.fold(0, |cnt, _x| cnt + 1)
}
}
}