Implement Pod and Zeroable for arrays of any size (#59)

* Implement Pod and Zeroable for arrays of any size

Only when rustc version is 1.51 or newer

* Rename cfg flag after feature name
This commit is contained in:
Zakarum 2021-04-02 04:56:49 +03:00 committed by GitHub
parent 30a96066fa
commit f3e16c7051
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 54 additions and 0 deletions

45
build.rs Normal file
View File

@ -0,0 +1,45 @@
use std::{
env,
process::Command,
str::{self, FromStr},
};
fn main() {
let minor = match rustc_minor_version() {
Some(minor) => minor,
None => return,
};
if minor >= 51 {
println!("cargo:rustc-cfg=min_const_generics");
}
}
fn rustc_minor_version() -> Option<u32> {
let rustc = match env::var_os("RUSTC") {
Some(rustc) => rustc,
None => return None,
};
let output = match Command::new(rustc).arg("--version").output() {
Ok(output) => output,
Err(_) => return None,
};
let version = match str::from_utf8(&output.stdout) {
Ok(version) => version,
Err(_) => return None,
};
let mut pieces = version.split('.');
if pieces.next() != Some("rustc 1") {
return None;
}
let next = match pieces.next() {
Some(next) => next,
None => return None,
};
u32::from_str(next).ok()
}

View File

@ -50,6 +50,7 @@ use core::{marker::*, mem::*, num::*, ptr::*};
#[doc(hidden)] #[doc(hidden)]
pub use ::core as __core; pub use ::core as __core;
#[cfg(not(generic_const))]
macro_rules! impl_unsafe_marker_for_array { macro_rules! impl_unsafe_marker_for_array {
( $marker:ident , $( $n:expr ),* ) => { ( $marker:ident , $( $n:expr ),* ) => {
$(unsafe impl<T> $marker for [T; $n] where T: $marker {})* $(unsafe impl<T> $marker for [T; $n] where T: $marker {})*

View File

@ -66,6 +66,10 @@ unsafe impl<T: Pod> Pod for ManuallyDrop<T> {}
// Note(Lokathor): MaybeUninit can NEVER be Pod. // Note(Lokathor): MaybeUninit can NEVER be Pod.
#[cfg(min_const_generics)]
unsafe impl<T, const N: usize> Pod for [T; N] where T: Pod {}
#[cfg(not(min_const_generics))]
impl_unsafe_marker_for_array!( impl_unsafe_marker_for_array!(
Pod, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, Pod, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 48, 64, 96, 128, 256, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 48, 64, 96, 128, 256,

View File

@ -109,6 +109,10 @@ unsafe impl<
{ {
} }
#[cfg(min_const_generics)]
unsafe impl<T, const N: usize> Zeroable for [T; N] where T: Zeroable {}
#[cfg(not(min_const_generics))]
impl_unsafe_marker_for_array!( impl_unsafe_marker_for_array!(
Zeroable, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, Zeroable, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 48, 64, 96, 128, 256, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 48, 64, 96, 128, 256,