News for hackers. Stuff that matters.
Re:Improving Rust compile times to enable adoption of memory... by nicoburns on Friday February 03, 2023 @08:54AM (#34641060)
There are a few more fundemental missing pieces for me:

- It's impossible to describe a type that "implements trait A and may or may not implement trait B"

- It's impossible to be generic over a trait (not a type that implements a trait, the trait itself)

Re:Improving Rust compile times to enable adoption of memory... by tialaramex on Friday February 03, 2023 @10:05AM (#34641938)
> It's impossible to describe a type that "implements trait A and may or may not implement trait B"

How is this different from just describing a type that only "implements trait A" ?

Re:Improving Rust compile times to enable adoption of memory... by nicoburns on Friday February 03, 2023 @10:10AM (#34642021)
It would allow you to call a function to check for trait B and downcast to "implements trait A and B" in the case that it does implement the trait.
Re:Improving Rust compile times to enable adoption of memory... by merely-unlikely on Friday February 03, 2023 @11:13AM (#34643072)
I’m still learning the language but couldn’t you use an enum containing two types to accomplish the same thing?
Re:Improving Rust compile times to enable adoption of memory... by nicoburns on Friday February 03, 2023 @02:53PM (#34646878)
You can if you know all of the possible types in advance. But if you want to expose this as an interface from a library that allows users to provide their own custom implementation then you need to use traits.
Re:Improving Rust compile times to enable adoption of memory... by tialaramex on Friday February 03, 2023 @04:55PM (#34648278)
It seems like a way to ask "Can this thing implement X and if so how?" from say the Any trait would be what you want here, I have no idea how hard that would be to deliver but I also don't see how the previous trait thing is relevant, like, why do we need to say up front that maybe we will care whether trait B is implemented?
Re:Improving Rust compile times to enable adoption of memory... by insanitybit on Friday February 03, 2023 @10:16AM (#34642125)
> - It's impossible to describe a type that "implements trait A and may or may not implement trait B"

So, specialization? Or something else? I haven't found a need for specialization. I remember when I came from C++ I had a hard time adjusting to "no specialization, no variadics" but idk I haven't missed it in years.

> - It's impossible to be generic over a trait (not a type that implements a trait, the trait itself)

Not sure I understand.

Re:Improving Rust compile times to enable adoption of memory... by nicoburns on Friday February 03, 2023 @11:16AM (#34643110)
> So, specialization?

Basically yes. But that works with dynamic dispatch (trait objects) as well as static dispatch (generics).

> Not sure I understand.

A specific pattern I'd like to be able to represent is:

    trait AlgorithmAInputData {
      ...
    }

    trait AlgorithmA {
      trait InputData = AlgorithmAInputData;
      ...
    }

    trait DataStorage<trait AlgorithmA> {
      type InputData : Algorithm::InputData;
      
      fn get_input_data() -> InputData;

    }

    fn compute_algorithm_a<Storage: DataStorage<AlgorithmA>>() {
      ...
    }