lib: use macros to coerce types

/cc @trevnorris - work-in-progress, just gauging if you feel it's going in the right direction.