mirror of
https://github.com/Lokathor/bytemuck.git
synced 2024-11-21 14:22:26 +00:00
Attach min_const_generics to a crate feature (#63)
* Attach min_const_generics to a crate feature * Remove `min_const_generics` detection logic
This commit is contained in:
parent
f3e16c7051
commit
cd8dbd64e9
@ -17,6 +17,7 @@ extern_crate_alloc = []
|
||||
extern_crate_std = ["extern_crate_alloc"]
|
||||
zeroable_maybe_uninit = []
|
||||
derive = ["bytemuck_derive"]
|
||||
min_const_generics = []
|
||||
|
||||
[dependencies]
|
||||
# use the upper line for testing against bytemuck_derive changes, if any
|
||||
|
45
build.rs
45
build.rs
@ -1,45 +0,0 @@
|
||||
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,7 +50,7 @@ use core::{marker::*, mem::*, num::*, ptr::*};
|
||||
#[doc(hidden)]
|
||||
pub use ::core as __core;
|
||||
|
||||
#[cfg(not(generic_const))]
|
||||
#[cfg(not(feature = "min_const_generics"))]
|
||||
macro_rules! impl_unsafe_marker_for_array {
|
||||
( $marker:ident , $( $n:expr ),* ) => {
|
||||
$(unsafe impl<T> $marker for [T; $n] where T: $marker {})*
|
||||
|
@ -66,10 +66,10 @@ unsafe impl<T: Pod> Pod for ManuallyDrop<T> {}
|
||||
|
||||
// Note(Lokathor): MaybeUninit can NEVER be Pod.
|
||||
|
||||
#[cfg(min_const_generics)]
|
||||
#[cfg(feature = "min_const_generics")]
|
||||
unsafe impl<T, const N: usize> Pod for [T; N] where T: Pod {}
|
||||
|
||||
#[cfg(not(min_const_generics))]
|
||||
#[cfg(not(feature = "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,
|
||||
|
@ -109,10 +109,10 @@ unsafe impl<
|
||||
{
|
||||
}
|
||||
|
||||
#[cfg(min_const_generics)]
|
||||
#[cfg(feature = "min_const_generics")]
|
||||
unsafe impl<T, const N: usize> Zeroable for [T; N] where T: Zeroable {}
|
||||
|
||||
#[cfg(not(min_const_generics))]
|
||||
#[cfg(not(feature = "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,
|
||||
|
12
tests/array_tests.rs
Normal file
12
tests/array_tests.rs
Normal file
@ -0,0 +1,12 @@
|
||||
#[test]
|
||||
pub fn test_cast_array() {
|
||||
let x = [0u32, 1u32, 2u32];
|
||||
let _: [u16; 6] = bytemuck::cast(x);
|
||||
}
|
||||
|
||||
#[cfg(feature = "min_const_generics")]
|
||||
#[test]
|
||||
pub fn test_cast_long_array() {
|
||||
let x = [0u32; 65];
|
||||
let _: [u16; 130] = bytemuck::cast(x);
|
||||
}
|
Loading…
Reference in New Issue
Block a user