static void RegisterCancelation<T>(AsyncSubject<T> subject, IDisposable subscription, CancellationToken token) { // // Separate method used to avoid heap allocation of closure when no cancellation is needed, // e.g. when CancellationToken.None is provided to the RunAsync overloads. // var ctr = token.Register(() => { subscription.Dispose(); Cancel(subject, token); }); // // No null-check for ctr is needed: // // - CancellationTokenRegistration is a struct // - Registration will succeed 99% of the time, no warranting an attempt to avoid spurious Subscribe calls // subject.Subscribe(Stubs<T>.Ignore, _ => ctr.Dispose(), ctr.Dispose); }