Skip to content

Commit f00ca1b

Browse files
committed
Create a new 'IntType' structure
Simple a pair of (IntType, signed) Replaces the fields of the old TypeInfo::Integer variant. This is a breaking change!
1 parent 5bdf052 commit f00ca1b

File tree

3 files changed

+168
-83
lines changed

3 files changed

+168
-83
lines changed

src/core.rs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Implementations of [StaticReflect] for core types (for `#![no_std]`)
2-
use crate::StaticReflect;
3-
use crate::types::{TypeInfo, SimpleNonZeroRepr};
2+
use crate::{StaticReflect, PrimInt, PrimFloat};
3+
use crate::types::{TypeInfo, IntSize, IntType, SimpleNonZeroRepr, FloatSize};
44
use std::mem::{self, ManuallyDrop};
55
use core::ptr::NonNull;
66
use std::num::{NonZeroI32, NonZeroU32, NonZeroU8, NonZeroUsize};
@@ -15,13 +15,15 @@ macro_rules! impl_primitive {
1515
macro_rules! impl_ints {
1616
($($target:ty),*) => {
1717
$(unsafe impl StaticReflect for $target {
18+
const TYPE_INFO: TypeInfo<'static> = TypeInfo::Integer(Self::INT_TYPE);
19+
}
20+
unsafe impl PrimInt for $target {
21+
const INT_SIZE: IntSize = IntSize::unwrap_from_bytes(std::mem::size_of::<Self>());
1822
#[allow(unused_comparisons)]
19-
const TYPE_INFO: TypeInfo<'static> = {
20-
let size = std::mem::size_of::<$target>();
21-
let signed = <$target>::MIN < 0;
22-
TypeInfo::integer(size, signed)
23-
};
24-
})*
23+
const SIGNED: bool = <$target>::MIN < 0;
24+
const INT_TYPE: IntType = IntType { size: Self::INT_SIZE, signed: Self::SIGNED };
25+
}
26+
impl crate::sealed::Sealed for $target {})*
2527
}
2628
}
2729
// NOTE: Pointer sized integers have machine-dependent implementation :(
@@ -33,6 +35,14 @@ impl_primitive!(() => TypeInfo::Unit);
3335
impl_primitive!(bool => TypeInfo::Bool);
3436
impl_primitive!(f32 => TypeInfo::F32);
3537
impl_primitive!(f64 => TypeInfo::F64);
38+
impl crate::sealed::Sealed for f32 {}
39+
unsafe impl PrimFloat for f32 {
40+
const FLOAT_SIZE: FloatSize = FloatSize::Single;
41+
}
42+
impl crate::sealed::Sealed for f64 {}
43+
unsafe impl PrimFloat for f64 {
44+
const FLOAT_SIZE: FloatSize = FloatSize::Double;
45+
}
3646

3747
// Builtin support for the never type
3848
impl_primitive!(! => TypeInfo::Never);

src/lib.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ mod core;
3232

3333
pub use crate::types::TypeInfo;
3434

35+
use crate::types::{IntType, IntSize, FloatSize};
36+
use std::ops::{Sub, Mul, Add};
37+
3538
/// The trait for types whose information can be accessed via static reflection.
3639
///
3740
/// In order to proper access any fields,
@@ -58,6 +61,24 @@ pub unsafe trait StaticReflect {
5861
const TYPE_INFO: TypeInfo<'static>;
5962
}
6063

64+
/// A primitive integer type
65+
pub unsafe trait PrimInt: StaticReflect + Copy + Add<Self> + Sub<Self> + Mul<Self> + sealed::Sealed {
66+
/// The [IntType] of this integer
67+
const INT_TYPE: IntType;
68+
/// Whether or not this integer is signed
69+
const SIGNED: bool;
70+
/// The size of this integer
71+
///
72+
/// If this integer is pointer-sized (`isize`/`usize`),
73+
/// then this will be its runtime size on the current platform.
74+
const INT_SIZE: IntSize;
75+
}
76+
/// A primitive float type
77+
pub unsafe trait PrimFloat: StaticReflect + Copy + sealed::Sealed {
78+
/// The size of this float
79+
const FLOAT_SIZE: FloatSize;
80+
}
81+
6182
/// A type that supports accessing its fields via reflection.
6283
///
6384
/// All fields are assumed to be defined in a way that is compatible
@@ -73,3 +94,7 @@ pub unsafe trait FieldReflect: StaticReflect {
7394
/// where each field's information is given by name
7495
const NAMED_FIELD_INFO: Self::NamedFieldInfo;
7596
}
97+
98+
mod sealed {
99+
pub trait Sealed {}
100+
}

0 commit comments

Comments
 (0)