3 ms·
- Raw-pointer equivalent to lifetime parameters (`GenericStruct<*>`, converts `&'a T` into `*const T` and `&'a mut T` into `*mut T`) - `impl Trait` for associa
by armchairhacker 4y ago
- Raw-pointer equivalent to lifetime parameters (`GenericStruct<*>`, converts `&'a T` into `*const T` and `&'a mut T` into `*mut T`)
- `impl Trait` for associated types. This lets you workaround async trait functions without boxing the returned futures and also return anonymous closures from traits
- And with that, async trait functions
trait Foo {
async fn hello() -> Bar;
}
impl Foo for Baz {
async fn hello() -> Bar { ... }
}
// desugars to
trait Foo {
type __Hello: Future<Output=Bar>;
fn hello() -> Self::__Hello;
}
impl Foo for Baz {
type __Hello = impl Future<Output=Bar>;
fn hello() -> Self::__Hello { ... }
}
- Limited support for `impl Trait` with different return types, by returning an enum on traits where all methods can be trivially implemented (see https://crates.io/crates/auto_enums https://crates.io/crates/auto_enums)
- Explicitly assert trait variance, and unsafely override trait variance.
- Improved compile-time support. In particular, enough support so that you can write functions that generate other functions at compile-time, e.g. `const fn pull_factory(repo_url: &str) -> impl Fn(&mut Repository)`, `const pull_my_repo = pull_factory("https://github.com/my-org/my-repo.git https://github.com/my-org/my-repo.git");`
- Remove restriction on `Self` parameters in trait objects, or at least allow `self: &Rc<Self>` and `self: &Arc<Self>`, as `self: Rc<Self>` and `self: Arc<Self>` are allowed
- Allow generics in trait objects if the generic parameter is only accessed / returned behind references and raw pointers, as then the compiler should be able to implement by generating 2 specialized variants: one for thin pointers and one for fat pointers.
- More declarative macro support, e.g. more types of ASTs as metavars
- Partial borrows: https://github.com/rust-lang/rfcs/issues/1215 https://github.com/rust-lang/rfcs/issues/1215
- Mutability variance: e.g. write `fn index<&a>(&a self, idx: Idx) -> &a T`, instead of needing to write `fn index(&self, idx: Idx) -> &T` and `fn index_mut(&mut self, idx: Idx) -> &mut T`)
- Built-in unit types (e.g. write `#[unit(ms)] pub struct Milliseconds(usize)`, and then you can write `128ms` => `Milliseconds(128)`)
- proto_lambda 4y ago> - Raw-pointer equivalent to lifetime parameters (`GenericStruct<*>`, converts `&'a T` into `*const T` and `&'a mut T` into `*mut T`) How would that work? References and pointers are fundamentally different types, and the primary operation on references (`Deref`) isn't available for pointers. Any methods implemented on the struct wouldn't be able to do anything with the "reference" field, since it might actually be a pointer instead - it would be like having a `struct Foo<T>(Vec<T>);`, and then specifying `Foo<boxed T>` (imaginary syntax) if you actually want a `Box<T>` instead of a `Vec<T>`. You can't just treat a `Box` like a `Vec` (or a pointer like a reference). > - `impl Trait` for associated types. This lets you workaround async trait functions without boxing the returned futures and also return anonymous closures from traits You're probably already aware, but for the bystanders, that seems to be close to happening: https://github.com/rust-lang/rust/issues/63063 https://github.com/rust-lang/rust/issues/63063 > - Mutability variance: e.g. write `fn index<&a>(&a self, idx: Idx) -> &a T`, instead of needing to write `fn index(&self, idx: Idx) -> &T` and `fn index_mut(&mut self, idx: Idx) -> &mut T`) I think that's something the Keyword Generics (aka "totally not effects") Initiative[1] is looking into. > - Built-in unit types (e.g. write `#[unit(ms)] pub struct Milliseconds(usize)`, and then you can write `128ms` => `Milliseconds(128)`) Making sure that doesn't turn into a name collision nightmare would probably needs some work. It might also make it very hard to figure out what type you're actually dealing with just by looking at such a number. The current solution to this problem, defining an extension trait on integers, produces only moderately more verbose code, with the benefit of not requiring any additional syntax: `128.ms()`. [1]: https://blog.rust-lang.org/inside-rust/2022/07/27/keyword-generics.html https://blog.rust-lang.org/inside-rust/2022/07/27/keyword-ge...