Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assert size_of type parameter greater than zero #16

Closed
wants to merge 1 commit into from

Conversation

spearman
Copy link

Partial fix for: #15

I couldn't figure out how to make this a compile-time error. It may be possible to do something with CTFE when that is more fleshed out.

@polyfractal
Copy link
Owner

Heya @spearman, sorry for letting this rot. Do you think this is still relevant? I haven't kept up with Rust lately, and haven't used the library actively for a while so you're a better judge than me :)

@spearman
Copy link
Author

There has been some progress in constant evaluation in the last few Rust versions, and there is now a static_assertions crate with some convenience macros: https://crates.io/crates/static_assertions, but I was not able to get this to work in this case for generic 'T':

fn make<T>(capacity: usize) -> (Producer<T>, Consumer<T>) {
const_assert!(std::mem::size_of::<T>() != 0);
...
---
error[E0277]: the trait bound `T: std::marker::Sized` is not satisfied
`T` does not have a constant size known at compile-time

One thing that appears to work at least for unit and zero-sized arrays is using an auto trait:

#![feature(optin_builtin_traits)]

pub auto trait Nonzero {}
impl !Nonzero for () {}
impl<T> !Nonzero for [T; 0] {}

...

fn make<T: Nonzero>(capacity: usize) -> (Producer<T>, Consumer<T>) {
...

So that covers only unit and zero-sized arrays, but not any user-created zero-sized types.

Really the main thing with this pull request is the assertion prints out a more informative error message, whereas before the program will just Segfault without explanation.

@knsd knsd mentioned this pull request Sep 22, 2018
@spearman
Copy link
Author

Closing in favor of #23

@spearman spearman closed this Mar 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants