News for hackers. Stuff that matters.
Re:Rust in QEMU Roadmap by duped on Tuesday December 03, 2024 @10:55AM (#42307526)
> You can't have Foo<N> * Foo<M> -> Foo<N+M> or similar.

You can though, unless I totally misunderstand your syntax

    use std::{marker::PhantomData, ops::{Add, Mul}};

    pub struct Foo<T> {
        _p: PhantomData<T>,
    }

    impl<N, M> Mul<Foo<M>> for Foo<N> 
    where
        N: Add<M>
    {
        type Output = Foo<<N as Add<M>>::Output>;
        fn mul(self, rhs: Foo<M>) -> Self::Output {
            todo!()
        }
    }
Re:Rust in QEMU Roadmap by ynik on Tuesday December 03, 2024 @12:12PM (#42308509)
You misunderstood. N, M are supposed to be integers (const generics); in your example code you've made them types. Also, your `type Output = Foo<<N as Add<M>>::Output>;` just means "multiplication has the same return type as addition". But desired is that multiplying a Foo<4> with a Foo<3> results in a Foo<7>.

Rust decided that it's important to not have instantiation-time compiler errors, but this makes computation with const generics complicated: you're not allowed to write Foo<{N+M}> because N+M might overflow, even if it never actually overflows for the generic arguments used in the program.

Re:Rust in QEMU Roadmap by quotemstr on Tuesday December 03, 2024 @02:02PM (#42309905)
Instantiation time errors unlock so much metaprogramming potential that Rust is going to be forced to allow them sooner or later.
Re:Rust in QEMU Roadmap by Ar-Curunir on Tuesday December 03, 2024 @03:32PM (#42310995)
I believe this kind of stuff is being worked on.
Re:Rust in QEMU Roadmap by duped on Tuesday December 03, 2024 @03:47PM (#42311189)
aiui this isn't inherently limited by instantiation-time error messages and is available on nightly today with the generic_const_exprs feature. It's in the pipeline.