Why Port 10k LOC to Rust

Lessons learned

Who?

Kai, hacker from Bochum.

Interested in cryptography and reverse engineering.

flanfly / @_cibo_

What?

First commit is from September 2011

Everything was done in C++11 and Qt 4 later 5

Heavy use of template meta programming

ensure(lambda.count(source(*p.first,graph)));
unsigned int rank = std::accumulate(p.first,p.second,
	 lambda.at(source(*p.first,graph)) + get_edge(*p.first,graph).second,
#ifdef _MSC_VER
	 [&](int acc,po::digraph<N,std::pair<int,int>>::edge_descriptor e)
#else
	 [&](int acc,typename po::digraph<N,std::pair<int,int>>::edge_descriptor e)
#endif
	 {
		 return std::max(acc,reinterpret_cast<int>(lambda.at(source(e,graph)) +
						get_edge(e,graph).second));
	 });

ensure(lambda.insert(std::make_pair(*i,rank)).second);

Rust has Sum Types

Rust

match instr {
	Instr::Phi(ref vec) => ...
	Instr::Add(ref a, ref b) => ...
	...
}

C++

struct vis : public boost::static_visitor<std::string>
{
	std::string operator()(const int_add<rvalue>&) const { ... }
	std::string operator()(const univ_phi<rvalue>&) const { ... }
	...
};
vis v;

boost::apply_visitor(v,i);

Rust has the Borrow Checker

template<typename T,typename D>
struct basic_loc
{
	const T* operator->(void) const { return read(); }
	const T& operator*(void) const { return *read(); }
	//T* operator->(void) { return write(); }
	//T& operator*(void) { return *write(); }

	const T* read(void) const
	{
		std::shared_ptr<loc_control<T>> cb = static_cast<const D*>(this)->control();

		if(!cb->has_object())
			cb->inner = unmarshal<T>(_uuid,cb->storage());
		if(!cb->object())
			throw std::runtime_error("reading deleted loc");
		return cb->object();
	}

	T& write(void) { .. }
}

The 5th Rewrite

The port to Rust started 7th of June and took ~4 months

No prior Rust experience

Straight forward translation from C++ to Rust

An enjoyable experience

Compiler messages are really helpful

Found iterator invalidation bugs along the way

Cargo makes development on different platforms easy

Some bad parts

GUI libraries are still basic

Not much documentation on how to use macros

No best practises on how to package Rust applications

Thanks for listening

panopticon.re

das-labor/panopticon

#panopticon on Freenode

@_cibo_

flanfly