From f3e16c7051b561b027c797823b3ca16e7133028d Mon Sep 17 00:00:00 2001 From: Zakarum Date: Fri, 2 Apr 2021 04:56:49 +0300 Subject: [PATCH] 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 --- build.rs | 45 +++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 1 + src/pod.rs | 4 ++++ src/zeroable.rs | 4 ++++ 4 files changed, 54 insertions(+) create mode 100644 build.rs diff --git a/build.rs b/build.rs new file mode 100644 index 0000000..6a6cadc --- /dev/null +++ b/build.rs @@ -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 { + 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() +} diff --git a/src/lib.rs b/src/lib.rs index c22daaa..1e783a5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -50,6 +50,7 @@ use core::{marker::*, mem::*, num::*, ptr::*}; #[doc(hidden)] pub use ::core as __core; +#[cfg(not(generic_const))] macro_rules! impl_unsafe_marker_for_array { ( $marker:ident , $( $n:expr ),* ) => { $(unsafe impl $marker for [T; $n] where T: $marker {})* diff --git a/src/pod.rs b/src/pod.rs index e5cc693..194e9fb 100644 --- a/src/pod.rs +++ b/src/pod.rs @@ -66,6 +66,10 @@ unsafe impl Pod for ManuallyDrop {} // Note(Lokathor): MaybeUninit can NEVER be Pod. +#[cfg(min_const_generics)] +unsafe impl Pod for [T; N] where T: Pod {} + +#[cfg(not(min_const_generics))] 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, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 48, 64, 96, 128, 256, diff --git a/src/zeroable.rs b/src/zeroable.rs index ebd412f..3beba1c 100644 --- a/src/zeroable.rs +++ b/src/zeroable.rs @@ -109,6 +109,10 @@ unsafe impl< { } +#[cfg(min_const_generics)] +unsafe impl Zeroable for [T; N] where T: Zeroable {} + +#[cfg(not(min_const_generics))] 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, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 48, 64, 96, 128, 256,