News for hackers. Stuff that matters.
Re:When zero cost abstractions aren't zero cost by erk__ on Monday August 09, 2021 @09:55PM (#28124441)
I think a proper solution would be to expose the trait `IsZero` [0] as that is used to figure out when it is correct to use `calloc` instead of having to do `malloc` -> `memset` [1]. I don't know if there is any plans for exposing that trait, but it would be a nice thing to see.

[0]: https://github.com/rust-lang/rust/blob/master/library/alloc/...

[1]: https://github.com/rust-lang/rust/blob/master/library/alloc/...

Re:When zero cost abstractions aren't zero cost by conradludgate on Tuesday August 10, 2021 @02:43AM (#28125768)
I think you don't need an IsZero trait. It needs to know if the bytes are zero. A custom implementation of IsZero might not hold that guarantee.

However, if the type is Copy and all the bytes are zeros, then you know it can be calloc

Re:When zero cost abstractions aren't zero cost by tialaramex on Tuesday August 10, 2021 @07:05AM (#28127293)
(As you likely know) Normally in Rust the trait is a gentleman's agreement. If my type does not have total Equivalence then I don't implement the Eq trait, and so if a type has Eq then it's promising to deliver total Equivalence. You can, in fact, just defy this and make types that do something "completely wrong" while implementing the syntax of the Trait and I provide a suite of them so that other people don't need to do that experiment:

https://crates.io/crates/misfortunate

However, Rust does have two other tricks up its sleeve:

1. Unsafe traits. You can declare a trait to be "unsafe". Implementations of this trait likewise need that "unsafe" keyword and it alerts the programmer that, as with unsafe function calls, they are responsible for actually getting the implementation details correct. Not many standard library traits are unsafe because that's a considerable burden, but a few are, including Send and Sync.

You wouldn't learn much by implementing unsafe traits wrongly in a library like misfortunate, it's the same lesson as a C++ library that scribbles on random memory addresses.

2. Rust knows whether you implemented a trait by hand or merely derived it. In a few cases it can be reasonable for the language to distinguish these cases for built-in traits, since in the former case it knows the derived trait does what is needed even though a manual implementation may not.

It is possible that future improvements to Const Generics will rely on this latter idea. So long as you derive Eq rather than implementing it, Rust can reason that your type really is suitable as a constant type parameter whereas misfortunate::Always is not suitable (it would cause mayhem if permitted because of its idea of what "equality" is) despite claiming to be Eq.

Re:When zero cost abstractions aren't zero cost by tialaramex on Friday August 13, 2021 @05:54AM (#28167166)
> Rust knows whether you implemented a trait by hand or merely derived it. In a few cases it can be reasonable for the language to distinguish these cases for built-in traits, since in the former case it knows the derived trait does what is needed

Too late to edit. This should clearly have said, in the latter case not the former case. In the hand implemented case the compiler has no idea whether your implementation has the desired properties.

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.
Re:When zero cost abstractions aren't zero cost by Measter on Tuesday August 10, 2021 @06:50AM (#28127205)
You mean by checking the bytes of the data directly kinda like in this godbolt example: https://godbolt.org/z/78cvfshva
Re:When zero cost abstractions aren't zero cost by the8472 on Tuesday August 10, 2021 @08:31AM (#28127998)
> However, if the type is Copy and all the bytes are zeros, then you know it can be calloc

That's not as easy to check as one would think since Copy types can still contain uninitialized bytes.

Re:When zero cost abstractions aren't zero cost by PoignardAzur on Tuesday August 10, 2021 @03:04PM (#28132836)
Yeah, but in the common case all the uninitialized bytes are in the padding at the end of the struct (since Rust can re-arrange fields).

Especially in a case like in the article, where the type is very small (one byte), and the obstacle is that the author is using a newtype.

Re:When zero cost abstractions aren't zero cost by the8472 on Tuesday August 10, 2021 @03:48PM (#28133362)
Knowing that they usually are at the end doesn't help since you still need to write a general runtime check that expresses "are all the bits for this type zero or uninit". Afaik currently there is no way to do this without causing UB. This would need some new intrinsic.