Replace ~ with Box and box where appropriate

Upgrades the code to the latest Rust version.