The formation of toroidal bubbles upon the collapse of transient cavities