Remove confusing associated constants from enumerations

next
Sayan Nandan 6 months ago
parent 739b2c8e76
commit 30689fbdb1
No known key found for this signature in database
GPG Key ID: 0EBD769024B24F0A

@ -43,7 +43,7 @@ use {
ql::dml::upd::{AssignmentExpression, UpdateStatement}, ql::dml::upd::{AssignmentExpression, UpdateStatement},
sync, sync,
}, },
util::compiler, util::compiler::{self, TaggedEnum},
}, },
std::mem, std::mem,
}; };
@ -185,7 +185,7 @@ unsafe fn dc_op_str_add(dc: &Datacell, rhs: Lit) -> (bool, Datacell) {
} }
static OPERATOR: [unsafe fn(&Datacell, Lit) -> (bool, Datacell); { static OPERATOR: [unsafe fn(&Datacell, Lit) -> (bool, Datacell); {
TagClass::MAX as usize * AssignmentOperator::VARIANTS TagClass::MAX_DSCR as usize * AssignmentOperator::VARIANT_COUNT
}] = [ }] = [
// bool // bool
dc_op_bool_ass, dc_op_bool_ass,
@ -230,7 +230,7 @@ static OPERATOR: [unsafe fn(&Datacell, Lit) -> (bool, Datacell); {
#[inline(always)] #[inline(always)]
const fn opc(opr: TagClass, ope: AssignmentOperator) -> usize { const fn opc(opr: TagClass, ope: AssignmentOperator) -> usize {
(AssignmentOperator::VARIANTS * opr.value_word()) + ope.value_word() (AssignmentOperator::VARIANT_COUNT * opr.value_word()) + ope.value_word()
} }
#[cfg(test)] #[cfg(test)]

@ -24,7 +24,17 @@
* *
*/ */
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, sky_macros::EnumMethods)] #[derive(
Debug,
PartialEq,
Eq,
PartialOrd,
Ord,
Clone,
Copy,
sky_macros::EnumMethods,
sky_macros::TaggedEnum,
)]
#[repr(u8)] #[repr(u8)]
pub enum AssignmentOperator { pub enum AssignmentOperator {
Assign = 0, Assign = 0,

@ -34,7 +34,18 @@ macro_rules! strid {
} }
#[repr(u8)] #[repr(u8)]
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash, PartialOrd, Ord, sky_macros::EnumMethods)] #[derive(
Debug,
PartialEq,
Eq,
Clone,
Copy,
Hash,
PartialOrd,
Ord,
sky_macros::EnumMethods,
sky_macros::TaggedEnum,
)]
pub enum TagClass { pub enum TagClass {
Bool = 0, Bool = 0,
UnsignedInt = 1, UnsignedInt = 1,

@ -54,7 +54,7 @@ pub enum ProtocolError {
handshake meta handshake meta
*/ */
#[derive(Debug, PartialEq, Eq, Clone, Copy, sky_macros::EnumMethods)] #[derive(Debug, PartialEq, Eq, Clone, Copy, sky_macros::EnumMethods, sky_macros::TaggedEnum)]
#[repr(u8)] #[repr(u8)]
/// the handshake version /// the handshake version
pub enum HandshakeVersion { pub enum HandshakeVersion {
@ -62,7 +62,7 @@ pub enum HandshakeVersion {
Original = 0, Original = 0,
} }
#[derive(Debug, PartialEq, Eq, Clone, Copy, sky_macros::EnumMethods)] #[derive(Debug, PartialEq, Eq, Clone, Copy, sky_macros::EnumMethods, sky_macros::TaggedEnum)]
#[repr(u8)] #[repr(u8)]
/// the skyhash protocol version /// the skyhash protocol version
pub enum ProtocolVersion { pub enum ProtocolVersion {
@ -70,7 +70,7 @@ pub enum ProtocolVersion {
Original = 0, Original = 0,
} }
#[derive(Debug, PartialEq, Eq, Clone, Copy, sky_macros::EnumMethods)] #[derive(Debug, PartialEq, Eq, Clone, Copy, sky_macros::EnumMethods, sky_macros::TaggedEnum)]
#[repr(u8)] #[repr(u8)]
/// the data exchange mode /// the data exchange mode
pub enum DataExchangeMode { pub enum DataExchangeMode {
@ -78,7 +78,7 @@ pub enum DataExchangeMode {
QueryTime = 0, QueryTime = 0,
} }
#[derive(Debug, PartialEq, Eq, Clone, Copy, sky_macros::EnumMethods)] #[derive(Debug, PartialEq, Eq, Clone, Copy, sky_macros::EnumMethods, sky_macros::TaggedEnum)]
#[repr(u8)] #[repr(u8)]
/// the query mode /// the query mode
pub enum QueryMode { pub enum QueryMode {
@ -283,11 +283,11 @@ impl<'a> CHandshake<'a> {
} }
let buf: [u8; CHandshake::INITIAL_READ] = unsafe { scanner.next_chunk() }; let buf: [u8; CHandshake::INITIAL_READ] = unsafe { scanner.next_chunk() };
let invalid_first_byte = buf[0] != Self::CLIENT_HELLO; let invalid_first_byte = buf[0] != Self::CLIENT_HELLO;
let invalid_hs_version = buf[1] > HandshakeVersion::MAX; let invalid_hs_version = buf[1] > HandshakeVersion::MAX_DSCR;
let invalid_proto_version = buf[2] > ProtocolVersion::MAX; let invalid_proto_version = buf[2] > ProtocolVersion::MAX_DSCR;
let invalid_exchange_mode = buf[3] > DataExchangeMode::MAX; let invalid_exchange_mode = buf[3] > DataExchangeMode::MAX_DSCR;
let invalid_query_mode = buf[4] > QueryMode::MAX; let invalid_query_mode = buf[4] > QueryMode::MAX_DSCR;
let invalid_auth_mode = buf[5] > AuthMode::MAX; let invalid_auth_mode = buf[5] > AuthMode::MAX_DSCR;
// check block // check block
if compiler::unlikely( if compiler::unlikely(
invalid_first_byte invalid_first_byte

@ -304,10 +304,10 @@ impl<H: HeaderV1Spec> HeaderV1<H> {
okay_server_version, okay_server_version,
okay_driver_version, okay_driver_version,
// 2.1.2 // 2.1.2
raw_host_os <= HostOS::MAX, raw_host_os <= HostOS::MAX_DSCR,
raw_host_arch <= HostArch::MAX, raw_host_arch <= HostArch::MAX_DSCR,
raw_host_ptr_width <= HostPointerWidth::MAX, raw_host_ptr_width <= HostPointerWidth::MAX_DSCR,
raw_host_endian <= HostEndian::MAX, raw_host_endian <= HostEndian::MAX_DSCR,
// 2.1.3 // 2.1.3
raw_file_class <= H::FileClass::MAX_DSCR, raw_file_class <= H::FileClass::MAX_DSCR,
raw_file_specifier <= H::FileSpecifier::MAX_DSCR, raw_file_specifier <= H::FileSpecifier::MAX_DSCR,

@ -95,7 +95,7 @@ pub mod cell {
impl StorageCellTypeID { impl StorageCellTypeID {
#[inline(always)] #[inline(always)]
pub const fn is_valid(d: u8) -> bool { pub const fn is_valid(d: u8) -> bool {
d <= Self::MAX d <= Self::MAX_DSCR
} }
unsafe fn into_selector(self) -> TagSelector { unsafe fn into_selector(self) -> TagSelector {
debug_assert!(self.value_u8() != Self::Null.value_u8()); debug_assert!(self.value_u8() != Self::Null.value_u8());
@ -451,7 +451,7 @@ impl<'a> PersistObject for ModelLayoutRef<'a> {
> as PersistObject>::obj_dec( > as PersistObject>::obj_dec(
scanner, super::map::MapIndexSizeMD(md.field_c as usize) scanner, super::map::MapIndexSizeMD(md.field_c as usize)
)?; )?;
let ptag = if md.p_key_tag > TagSelector::MAX as u64 { let ptag = if md.p_key_tag > TagSelector::MAX_DSCR as u64 {
return Err(StorageError::InternalDecodeStructureCorruptedPayload.into()); return Err(StorageError::InternalDecodeStructureCorruptedPayload.into());
} else { } else {
TagSelector::from_raw(md.p_key_tag as u8) TagSelector::from_raw(md.p_key_tag as u8)

@ -431,7 +431,7 @@ impl DriverEvent {
checksum_ checksum_
.update(&block[Self::OFFSET_3_PAYLOAD_LEN.start..Self::OFFSET_6_LAST_TXN_ID.end]); .update(&block[Self::OFFSET_3_PAYLOAD_LEN.start..Self::OFFSET_6_LAST_TXN_ID.end]);
let target_checksum = checksum_.finish(); let target_checksum = checksum_.finish();
let invalid_ev_dscr = driver_event_ > DriverEventKind::MAX as u64; let invalid_ev_dscr = driver_event_ > DriverEventKind::MAX_DSCR as u64;
let invalid_ck = checksum != target_checksum; let invalid_ck = checksum != target_checksum;
let invalid_pl_size = payload_len != 3; let invalid_pl_size = payload_len != 3;
if invalid_ev_dscr | invalid_ck | invalid_pl_size { if invalid_ev_dscr | invalid_ck | invalid_pl_size {

@ -141,31 +141,16 @@ pub fn derive_tagged_enum(input: TokenStream) -> TokenStream {
#[proc_macro_derive(EnumMethods)] #[proc_macro_derive(EnumMethods)]
pub fn derive_value_methods(input: TokenStream) -> TokenStream { pub fn derive_value_methods(input: TokenStream) -> TokenStream {
let ast = parse_macro_input!(input as DeriveInput); let ast = parse_macro_input!(input as DeriveInput);
let (enum_name, repr_type, value_expressions, variant_len, repr_type_ident) = let (enum_name, repr_type, _, _, repr_type_ident) = process_enum_tags(&ast);
process_enum_tags(&ast);
let repr_type_ident_func = syn::Ident::new( let repr_type_ident_func = syn::Ident::new(
&format!("value_{repr_type}"), &format!("value_{repr_type}"),
proc_macro2::Span::call_site(), proc_macro2::Span::call_site(),
); );
let gen = quote! { let gen = quote! {
impl #enum_name { impl #enum_name {
pub const MAX: #repr_type_ident = Self::max_value();
pub const VARIANTS: usize = #variant_len;
pub const fn #repr_type_ident_func(&self) -> #repr_type_ident { unsafe { core::mem::transmute(*self) } } pub const fn #repr_type_ident_func(&self) -> #repr_type_ident { unsafe { core::mem::transmute(*self) } }
pub const fn value_word(&self) -> usize { self.#repr_type_ident_func() as usize } pub const fn value_word(&self) -> usize { self.#repr_type_ident_func() as usize }
pub const fn value_qword(&self) -> u64 { self.#repr_type_ident_func() as u64 } pub const fn value_qword(&self) -> u64 { self.#repr_type_ident_func() as u64 }
pub const fn max_value() -> #repr_type_ident {
let values = #value_expressions;
let mut i = 1;
let mut max = values[0];
while i < values.len() {
if values[i] > max {
max = values[i];
}
i = i + 1;
}
max
}
} }
}; };
gen.into() gen.into()

Loading…
Cancel
Save