next.js
e487b9ff - refactor(turbo-tasks): Simplify most type bounds on Vc<T> and related types (#72823)

Commit
1 year ago
refactor(turbo-tasks): Simplify most type bounds on Vc<T> and related types (#72823) `Vc<T>` had a type bound that `T: Send + ?Sized`. The general recommendation is to remove all type bounds from the struct that aren't necessary: - https://rust-lang.github.io/api-guidelines/future-proofing.html#c-struct-bounds - https://github.com/rust-lang/api-guidelines/issues/6 - https://github.com/rust-lang/rust-clippy/issues/1689 The reasoning is that type bounds on structs are "infectious". Any generic function, trait, or struct referring to `Vc<T>` was required to add `T: Send` bounds. *Sidenote: The [`implied_bounds` feature](https://rust-lang.github.io/rfcs/2089-implied-bounds.html) might mitigate some of this if ever stabilized.* Removing the `T: Send` type bound from `Vc<T>` means that there's less places where we need to specify it. This pattern can be seen in many parts of the stdlib. For example, `Arc<T>` doesn't require that `T: Send + Sync`, though in reality to do anything useful with it, you need `T: Send + Sync`. ## Is this safe? Yes, bounds are checked during cell construction, and the lower-level APIs used for creating cells require `Send + Sync`, so this would be hard to mess up without explicitly unsafe code. Also, `Vc<T>` also technically requires that `T: Sync`, but that wasn't enforced in the struct definition (only during cell construction). We did just fine without that bound on the struct. ## Why are you leaving `?Sized`? There's an implicit `Sized` bound on type parameters in struct definitions unless explicitly specified otherwise with `?Sized`. Right now this bound isn't used. We use a box, e.g. `Vc<Box<dyn Foo>>`, but in the future we might be able to drop that intermediate box from the type signature. I'm leaving the `?Sized` bound in place (and adding it in a few places where it was missing) in hopes of that.
Author
bgw bgw
Parents
Loading