1234567891011121314151617181920212223242526classvirtualt=objectmethodvirtualrun:'a.(cancelled:exnPromise.t->'a)->'amethodvirtualrun_raw:'a.(unit->'a)->'aendletrun_raw(t:#t)=t#run_rawletrun(t:#t)fn=t#run@@fun~cancelled->(* If the spawning fiber is cancelled, [cancelled] gets set to the exception. *)tryFiber.first(fun()->matchPromise.awaitcancelledwith|Cancel.Cancelledex->raiseex(* To avoid [Cancelled (Cancelled ex))] *)|ex->raiseex(* Shouldn't happen *))fnwithex->matchPromise.peekcancelledwith|Some(Cancel.Cancelledex2ascex)whenex==ex2->(* We unwrapped the exception above to avoid [fn] seeing a double cancelled exception.
But this means that the top-level reported the original exception,
which isn't what we want. *)raisecex|_->raiseex