Skip to content

Commit

Permalink
Remove confusing associated constants from enumerations
Browse files Browse the repository at this point in the history
  • Loading branch information
ohsayan committed Mar 11, 2024
1 parent 739b2c8 commit 30689fb
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 37 deletions.
6 changes: 3 additions & 3 deletions server/src/engine/core/dml/upd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ use {
ql::dml::upd::{AssignmentExpression, UpdateStatement},
sync,
},
util::compiler,
util::compiler::{self, TaggedEnum},
},
std::mem,
};
Expand Down Expand Up @@ -185,7 +185,7 @@ unsafe fn dc_op_str_add(dc: &Datacell, rhs: 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
dc_op_bool_ass,
Expand Down Expand Up @@ -230,7 +230,7 @@ static OPERATOR: [unsafe fn(&Datacell, Lit) -> (bool, Datacell); {

#[inline(always)]
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)]
Expand Down
12 changes: 11 additions & 1 deletion server/src/engine/core/query_meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
pub enum AssignmentOperator {
Assign = 0,
Expand Down
13 changes: 12 additions & 1 deletion server/src/engine/data/tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,18 @@ macro_rules! strid {
}

#[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 {
Bool = 0,
UnsignedInt = 1,
Expand Down
18 changes: 9 additions & 9 deletions server/src/engine/net/protocol/handshake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,31 +54,31 @@ pub enum ProtocolError {
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)]
/// the handshake version
pub enum HandshakeVersion {
/// Skyhash/2.0 HS
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)]
/// the skyhash protocol version
pub enum ProtocolVersion {
/// Skyhash/2.0 protocol
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)]
/// the data exchange mode
pub enum DataExchangeMode {
/// query-time data exchange mode
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)]
/// the query mode
pub enum QueryMode {
Expand Down Expand Up @@ -283,11 +283,11 @@ impl<'a> CHandshake<'a> {
}
let buf: [u8; CHandshake::INITIAL_READ] = unsafe { scanner.next_chunk() };
let invalid_first_byte = buf[0] != Self::CLIENT_HELLO;
let invalid_hs_version = buf[1] > HandshakeVersion::MAX;
let invalid_proto_version = buf[2] > ProtocolVersion::MAX;
let invalid_exchange_mode = buf[3] > DataExchangeMode::MAX;
let invalid_query_mode = buf[4] > QueryMode::MAX;
let invalid_auth_mode = buf[5] > AuthMode::MAX;
let invalid_hs_version = buf[1] > HandshakeVersion::MAX_DSCR;
let invalid_proto_version = buf[2] > ProtocolVersion::MAX_DSCR;
let invalid_exchange_mode = buf[3] > DataExchangeMode::MAX_DSCR;
let invalid_query_mode = buf[4] > QueryMode::MAX_DSCR;
let invalid_auth_mode = buf[5] > AuthMode::MAX_DSCR;
// check block
if compiler::unlikely(
invalid_first_byte
Expand Down
8 changes: 4 additions & 4 deletions server/src/engine/storage/common/sdss/impls/sdss_r1/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,10 +304,10 @@ impl<H: HeaderV1Spec> HeaderV1<H> {
okay_server_version,
okay_driver_version,
// 2.1.2
raw_host_os <= HostOS::MAX,
raw_host_arch <= HostArch::MAX,
raw_host_ptr_width <= HostPointerWidth::MAX,
raw_host_endian <= HostEndian::MAX,
raw_host_os <= HostOS::MAX_DSCR,
raw_host_arch <= HostArch::MAX_DSCR,
raw_host_ptr_width <= HostPointerWidth::MAX_DSCR,
raw_host_endian <= HostEndian::MAX_DSCR,
// 2.1.3
raw_file_class <= H::FileClass::MAX_DSCR,
raw_file_specifier <= H::FileSpecifier::MAX_DSCR,
Expand Down
4 changes: 2 additions & 2 deletions server/src/engine/storage/common_encoding/r1/obj.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ pub mod cell {
impl StorageCellTypeID {
#[inline(always)]
pub const fn is_valid(d: u8) -> bool {
d <= Self::MAX
d <= Self::MAX_DSCR
}
unsafe fn into_selector(self) -> TagSelector {
debug_assert!(self.value_u8() != Self::Null.value_u8());
Expand Down Expand Up @@ -451,7 +451,7 @@ impl<'a> PersistObject for ModelLayoutRef<'a> {
> as PersistObject>::obj_dec(
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());
} else {
TagSelector::from_raw(md.p_key_tag as u8)
Expand Down
2 changes: 1 addition & 1 deletion server/src/engine/storage/v2/raw/journal/raw/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ impl DriverEvent {
checksum_
.update(&block[Self::OFFSET_3_PAYLOAD_LEN.start..Self::OFFSET_6_LAST_TXN_ID.end]);
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_pl_size = payload_len != 3;
if invalid_ev_dscr | invalid_ck | invalid_pl_size {
Expand Down
17 changes: 1 addition & 16 deletions sky-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,31 +141,16 @@ pub fn derive_tagged_enum(input: TokenStream) -> TokenStream {
#[proc_macro_derive(EnumMethods)]
pub fn derive_value_methods(input: TokenStream) -> TokenStream {
let ast = parse_macro_input!(input as DeriveInput);
let (enum_name, repr_type, value_expressions, variant_len, repr_type_ident) =
process_enum_tags(&ast);
let (enum_name, repr_type, _, _, repr_type_ident) = process_enum_tags(&ast);
let repr_type_ident_func = syn::Ident::new(
&format!("value_{repr_type}"),
proc_macro2::Span::call_site(),
);
let gen = quote! {
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 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 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()
Expand Down

0 comments on commit 30689fb

Please sign in to comment.