refs: add a "for_each_replace_ref" function