mirror of
https://github.com/Lokathor/bytemuck.git
synced 2024-11-21 22:32:23 +00:00
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:
parent
30a96066fa
commit
f3e16c7051
45
build.rs
Normal file
45
build.rs
Normal 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()
|
||||||
|
}
|
@ -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 {})*
|
||||||
|
@ -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,
|
||||||
|
@ -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,
|
||||||
|
Loading…
Reference in New Issue
Block a user