News for hackers. Stuff that matters.
Re:When zero cost abstractions aren't zero cost by conradludgate on Tuesday August 10, 2021 @09:38AM (#28128675)
Good idea with unsafe traits. Maybe something like

  unsafe trait IsZero: Copy {
    fn is_zero(&self) -> bool;
  }

  #[derive(Copy, IsZero)]
  struct Foo {
    a: u8,
    b: u16,
  }
which could derive into

  unsafe impl IsZero for Foo {
    fn is_zero(&self) -> bool {
      true
        && self.a.is_zero()
        && self.b.is_zero()
    }
  }
Re:When zero cost abstractions aren't zero cost by conradludgate on Tuesday August 10, 2021 @09:41AM (#28128711)
There needs to be some thought into how it works with Enums too. Rust currently has no built in standard for enum defaults (you have to implement default yourself). It would be nice to somehow mark one of the variants to be represented with the 0 discriminant (Like how Option::None is implemented)
Re:When zero cost abstractions aren't zero cost by estebank on Tuesday August 10, 2021 @10:02AM (#28128987)
The RFC for that has been recently accepted[1] and the implementation is already available in nightly[2].

[1]: https://rust-lang.github.io/rfcs/3107-derive-enum-default.ht...

[2]: https://github.com/rust-lang/rust/pull/86735

Re:When zero cost abstractions aren't zero cost by conradludgate on Tuesday August 10, 2021 @11:07AM (#28129755)
Unfortunately there's still no guarantee for which variant will be represented by 0
Re:When zero cost abstractions aren't zero cost by erk__ on Tuesday August 10, 2021 @11:31AM (#28130055)
You may also have cases where no variant is 0 so this case is probably not as straightforward
Re:When zero cost abstractions aren't zero cost by estebank on Tuesday August 10, 2021 @11:54AM (#28130390)
The comment is fair. There have been some talks about being able to allow end users specify niches so that if you could have something like

    enum E<T> {
        Empty = 42,
        Some(NonFortyTwo<T>),
    }
in the same way that the following is of size T today

    enum E<T> {
        Empty = 0,
        Some(NonZero<T>),
    }
but that's not possible today.
Re:When zero cost abstractions aren't zero cost by tialaramex on Tuesday August 10, 2021 @01:45PM (#28131887)
Yes, I can see uses for this. Forty-two is obviously not something you'd actually want, but I can see needing numbers in the range 0-100 and being frustrated that since I need 0 I can't carve out a niche even though I don't mind losing 101 through 255 from a u8 instead of making my type wider.

Still the existing tricks get a lot done for relatively little work. I have used Option<NonZeroUsize> to do roughly what I'd use a single integer for in C, but making explicit that zero isn't just zero, but "I dunno, invalid". Would I have done that even if it cost more space? Probably, but it was cool that I didn't even need to consider that, "Zero cost abstraction".

Re:When zero cost abstractions aren't zero cost by erk__ on Tuesday August 10, 2021 @11:36AM (#28130131)
Yeah something like this would be how I would like to see it, the trait is already there and is pretty much that (without the Copy bound though as that is not needed.) Hopefully it will end up public and can be implemented with a derive.