Skip to content

Commit

Permalink
Merge pull request rust-lang#418 from mbrubeck/const
Browse files Browse the repository at this point in the history
Remove use of unstable const_type_id feature (fixes CI failures on 1.47+)
  • Loading branch information
KodrAus committed Oct 15, 2020
2 parents 7a42b05 + 14335ec commit 14d7303
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 79 deletions.
3 changes: 1 addition & 2 deletions build.rs
Expand Up @@ -2,8 +2,7 @@
//! atomics and sets `cfg` flags accordingly.

use std::env;
use std::process::Command;
use std::str::{self, FromStr};
use std::str;

#[cfg(feature = "kv_unstable")]
#[path = "src/kv/value/internal/cast/primitive.rs"]
Expand Down
79 changes: 2 additions & 77 deletions src/kv/value/internal/cast/primitive.rs
Expand Up @@ -13,83 +13,8 @@ In the future when `min_specialization` is stabilized we could use it instead an
the `'static` bound altogether.
*/

// Use consts to match a type with a conversion fn
#[cfg(all(srcbuild, const_type_id))]
pub(super) fn from_any<'v, T: ?Sized + 'static>(
value: &'v T,
) -> Option<crate::kv::value::internal::Primitive<'v>> {
use std::any::TypeId;

use crate::kv::value::internal::Primitive;

macro_rules! to_primitive {
($($ty:ty : ($const_ident:ident, $option_ident:ident),)*) => {
trait ToPrimitive
where
Self: 'static,
{
const CALL: fn(&Self) -> Option<Primitive> = {
$(
const $const_ident: TypeId = TypeId::of::<$ty>();
const $option_ident: TypeId = TypeId::of::<Option<$ty>>();
);*

match TypeId::of::<Self>() {
$(
$const_ident => |v| Some(Primitive::from(unsafe { *(v as *const Self as *const $ty) })),
$option_ident => |v| Some({
let v = unsafe { *(v as *const Self as *const Option<$ty>) };
match v {
Some(v) => Primitive::from(v),
None => Primitive::None,
}
}),
)*

_ => |_| None,
}
};

fn to_primitive(&self) -> Option<Primitive> {
(Self::CALL)(self)
}
}

impl<T: ?Sized + 'static> ToPrimitive for T {}
}
}

// NOTE: The types here *must* match the ones used below when `const_type_id` is not available
to_primitive![
usize: (USIZE, OPTION_USIZE),
u8: (U8, OPTION_U8),
u16: (U16, OPTION_U16),
u32: (U32, OPTION_U32),
u64: (U64, OPTION_U64),

isize: (ISIZE, OPTION_ISIZE),
i8: (I8, OPTION_I8),
i16: (I16, OPTION_I16),
i32: (I32, OPTION_I32),
i64: (I64, OPTION_I64),

f32: (F32, OPTION_F32),
f64: (F64, OPTION_F64),

char: (CHAR, OPTION_CHAR),
bool: (BOOL, OPTION_BOOL),
&'static str: (STR, OPTION_STR),
];

value.to_primitive()
}

#[cfg(all(not(src_build), const_type_id))]
#[allow(dead_code)]
pub fn generate() {}

// Use a build-time generated set of type ids to match a type with a conversion fn
#[cfg(all(srcbuild, not(const_type_id)))]
#[cfg(srcbuild)]
pub(super) fn from_any<'v>(
value: &'v (dyn std::any::Any + 'static),
) -> Option<crate::kv::value::internal::Primitive<'v>> {
Expand All @@ -107,7 +32,7 @@ pub(super) fn from_any<'v>(
}

// When the `src_build` config is not set then we're in the build script
#[cfg(all(not(srcbuild), not(const_type_id)))]
#[cfg(not(srcbuild))]
#[allow(dead_code)]
pub fn generate() {
use std::path::Path;
Expand Down

0 comments on commit 14d7303

Please sign in to comment.