diff --git a/generator/src/generator/namespace/switch.rs b/generator/src/generator/namespace/switch.rs index 8f4047ff..c2f07b97 100644 --- a/generator/src/generator/namespace/switch.rs +++ b/generator/src/generator/namespace/switch.rs @@ -261,6 +261,9 @@ pub(super) fn emit_switch_type( outln!(out, "}}"); } + let generate_switch_expr_fn = + generate_serialize && switch.cases.iter().all(|case| case.exprs.len() == 1); + if generate_serialize { if let Some(size) = switch.size() { emit_fixed_size_switch_serialize( @@ -279,14 +282,12 @@ pub(super) fn emit_switch_type( name, &case_infos, switch_expr_type, + generate_switch_expr_fn, out, ); } } - let generate_switch_expr_fn = - generate_serialize && switch.cases.iter().all(|case| case.exprs.len() == 1); - if generate_switch_expr_fn { outln!(out, "impl {} {{", name); out.indented(|out| { @@ -769,6 +770,7 @@ fn emit_variable_size_switch_serialize( name: &str, case_infos: &[CaseInfo], switch_expr_type: &str, + has_switch_expr_fn: bool, out: &mut Output, ) { let external_params = switch.external_params.borrow(); @@ -808,21 +810,23 @@ fn emit_variable_size_switch_serialize( ext_params_arg_defs ); out.indented(|out| { - // TODO: emit an assertion checking that the switch case has - // been set properly - // - // omitted here because some expressions do not have a - // `switch_expr()` function - let _ = switch_expr_type; - - // eat the parameters for now - if !external_params.is_empty() { - for external_param in external_params.iter() { - outln!( - out, - "let _ = {};", - to_rust_variable_name(&external_param.name) - ); + if has_switch_expr_fn { + serialize::emit_assert_for_switch_serialize( + generator, + switch, + switch_expr_type, + out, + ); + } else { + // eat unused parameters + if !external_params.is_empty() { + for external_param in external_params.iter() { + outln!( + out, + "let _ = {};", + to_rust_variable_name(&external_param.name) + ); + } } } diff --git a/x11rb-protocol/src/protocol/render.rs b/x11rb-protocol/src/protocol/render.rs index 5e695e94..dfcdbd2e 100644 --- a/x11rb-protocol/src/protocol/render.rs +++ b/x11rb-protocol/src/protocol/render.rs @@ -1820,7 +1820,7 @@ impl CreatePictureAux { result } fn serialize_into(&self, bytes: &mut Vec, value_mask: u32) { - let _ = value_mask; + assert_eq!(self.switch_expr(), u32::from(value_mask), "switch `value_list` has an inconsistent discriminant"); if let Some(repeat) = self.repeat { u32::from(repeat).serialize_into(bytes); } @@ -2226,7 +2226,7 @@ impl ChangePictureAux { result } fn serialize_into(&self, bytes: &mut Vec, value_mask: u32) { - let _ = value_mask; + assert_eq!(self.switch_expr(), u32::from(value_mask), "switch `value_list` has an inconsistent discriminant"); if let Some(repeat) = self.repeat { u32::from(repeat).serialize_into(bytes); } diff --git a/x11rb-protocol/src/protocol/screensaver.rs b/x11rb-protocol/src/protocol/screensaver.rs index 5e4b22d8..e2268742 100644 --- a/x11rb-protocol/src/protocol/screensaver.rs +++ b/x11rb-protocol/src/protocol/screensaver.rs @@ -727,7 +727,7 @@ impl SetAttributesAux { result } fn serialize_into(&self, bytes: &mut Vec, value_mask: u32) { - let _ = value_mask; + assert_eq!(self.switch_expr(), u32::from(value_mask), "switch `value_list` has an inconsistent discriminant"); if let Some(background_pixmap) = self.background_pixmap { background_pixmap.serialize_into(bytes); } diff --git a/x11rb-protocol/src/protocol/sync.rs b/x11rb-protocol/src/protocol/sync.rs index 42d0bb3c..80ac80bf 100644 --- a/x11rb-protocol/src/protocol/sync.rs +++ b/x11rb-protocol/src/protocol/sync.rs @@ -1228,7 +1228,7 @@ impl CreateAlarmAux { result } fn serialize_into(&self, bytes: &mut Vec, value_mask: u32) { - let _ = value_mask; + assert_eq!(self.switch_expr(), u32::from(value_mask), "switch `value_list` has an inconsistent discriminant"); if let Some(counter) = self.counter { counter.serialize_into(bytes); } @@ -1467,7 +1467,7 @@ impl ChangeAlarmAux { result } fn serialize_into(&self, bytes: &mut Vec, value_mask: u32) { - let _ = value_mask; + assert_eq!(self.switch_expr(), u32::from(value_mask), "switch `value_list` has an inconsistent discriminant"); if let Some(counter) = self.counter { counter.serialize_into(bytes); } diff --git a/x11rb-protocol/src/protocol/xinput.rs b/x11rb-protocol/src/protocol/xinput.rs index eaadd30f..bdf0b446 100644 --- a/x11rb-protocol/src/protocol/xinput.rs +++ b/x11rb-protocol/src/protocol/xinput.rs @@ -860,7 +860,7 @@ impl InputInfoInfo { result } fn serialize_into(&self, bytes: &mut Vec, class_id: u8) { - let _ = class_id; + assert_eq!(self.switch_expr(), u8::from(class_id), "switch `info` has an inconsistent discriminant"); match self { InputInfoInfo::Key(key) => key.serialize_into(bytes), InputInfoInfo::Button(button) => button.serialize_into(bytes), @@ -4336,7 +4336,7 @@ impl FeedbackStateData { result } fn serialize_into(&self, bytes: &mut Vec, class_id: u8) { - let _ = class_id; + assert_eq!(self.switch_expr(), u8::from(class_id), "switch `data` has an inconsistent discriminant"); match self { FeedbackStateData::Keyboard(keyboard) => keyboard.serialize_into(bytes), FeedbackStateData::Pointer(pointer) => pointer.serialize_into(bytes), @@ -5248,7 +5248,7 @@ impl FeedbackCtlData { result } fn serialize_into(&self, bytes: &mut Vec, class_id: u8) { - let _ = class_id; + assert_eq!(self.switch_expr(), u8::from(class_id), "switch `data` has an inconsistent discriminant"); match self { FeedbackCtlData::Keyboard(keyboard) => keyboard.serialize_into(bytes), FeedbackCtlData::Pointer(pointer) => pointer.serialize_into(bytes), @@ -6732,7 +6732,7 @@ impl InputStateData { result } fn serialize_into(&self, bytes: &mut Vec, class_id: u8) { - let _ = class_id; + assert_eq!(self.switch_expr(), u8::from(class_id), "switch `data` has an inconsistent discriminant"); match self { InputStateData::Key(key) => key.serialize_into(bytes), InputStateData::Button(button) => button.serialize_into(bytes), @@ -7852,7 +7852,7 @@ impl DeviceStateData { result } fn serialize_into(&self, bytes: &mut Vec, control_id: u16) { - let _ = control_id; + assert_eq!(self.switch_expr(), u16::from(control_id), "switch `data` has an inconsistent discriminant"); match self { DeviceStateData::Resolution(resolution) => resolution.serialize_into(bytes), DeviceStateData::AbsCalib(abs_calib) => abs_calib.serialize_into(bytes), @@ -8673,7 +8673,7 @@ impl DeviceCtlData { result } fn serialize_into(&self, bytes: &mut Vec, control_id: u16) { - let _ = control_id; + assert_eq!(self.switch_expr(), u16::from(control_id), "switch `data` has an inconsistent discriminant"); match self { DeviceCtlData::Resolution(resolution) => resolution.serialize_into(bytes), DeviceCtlData::AbsCalib(abs_calib) => abs_calib.serialize_into(bytes), @@ -9148,8 +9148,7 @@ impl ChangeDevicePropertyAux { result } fn serialize_into(&self, bytes: &mut Vec, format: u8, num_items: u32) { - let _ = format; - let _ = num_items; + assert_eq!(self.switch_expr(), u8::from(format), "switch `items` has an inconsistent discriminant"); match self { ChangeDevicePropertyAux::Data8(data8) => { assert_eq!(data8.len(), usize::try_from(num_items).unwrap(), "`data8` has an incorrect length"); @@ -9524,8 +9523,7 @@ impl GetDevicePropertyItems { result } fn serialize_into(&self, bytes: &mut Vec, format: u8, num_items: u32) { - let _ = format; - let _ = num_items; + assert_eq!(self.switch_expr(), u8::from(format), "switch `items` has an inconsistent discriminant"); match self { GetDevicePropertyItems::Data8(data8) => { assert_eq!(data8.len(), usize::try_from(num_items).unwrap(), "`data8` has an incorrect length"); @@ -10699,7 +10697,7 @@ impl HierarchyChangeData { result } fn serialize_into(&self, bytes: &mut Vec, type_: u16) { - let _ = type_; + assert_eq!(self.switch_expr(), u16::from(type_), "switch `data` has an inconsistent discriminant"); match self { HierarchyChangeData::AddMaster(add_master) => add_master.serialize_into(bytes), HierarchyChangeData::RemoveMaster(remove_master) => remove_master.serialize_into(bytes), @@ -12469,7 +12467,7 @@ impl DeviceClassData { result } fn serialize_into(&self, bytes: &mut Vec, type_: u16) { - let _ = type_; + assert_eq!(self.switch_expr(), u16::from(type_), "switch `data` has an inconsistent discriminant"); match self { DeviceClassData::Key(key) => key.serialize_into(bytes), DeviceClassData::Button(button) => button.serialize_into(bytes), @@ -14162,8 +14160,7 @@ impl XIChangePropertyAux { result } fn serialize_into(&self, bytes: &mut Vec, format: u8, num_items: u32) { - let _ = format; - let _ = num_items; + assert_eq!(self.switch_expr(), u8::from(format), "switch `items` has an inconsistent discriminant"); match self { XIChangePropertyAux::Data8(data8) => { assert_eq!(data8.len(), usize::try_from(num_items).unwrap(), "`data8` has an incorrect length"); @@ -14537,8 +14534,7 @@ impl XIGetPropertyItems { result } fn serialize_into(&self, bytes: &mut Vec, format: u8, num_items: u32) { - let _ = format; - let _ = num_items; + assert_eq!(self.switch_expr(), u8::from(format), "switch `items` has an inconsistent discriminant"); match self { XIGetPropertyItems::Data8(data8) => { assert_eq!(data8.len(), usize::try_from(num_items).unwrap(), "`data8` has an incorrect length"); diff --git a/x11rb-protocol/src/protocol/xkb.rs b/x11rb-protocol/src/protocol/xkb.rs index 76a4e01f..57c62134 100644 --- a/x11rb-protocol/src/protocol/xkb.rs +++ b/x11rb-protocol/src/protocol/xkb.rs @@ -6442,9 +6442,7 @@ impl SelectEventsAux { result } fn serialize_into(&self, bytes: &mut Vec, affect_which: u16, clear: u16, select_all: u16) { - let _ = affect_which; - let _ = clear; - let _ = select_all; + assert_eq!(self.switch_expr(), u16::from(affect_which) & ((!u16::from(clear)) & (!u16::from(select_all))), "switch `details` has an inconsistent discriminant"); if let Some(ref bitcase1) = self.bitcase1 { bitcase1.serialize_into(bytes); } @@ -7991,16 +7989,7 @@ impl GetMapMap { result } fn serialize_into(&self, bytes: &mut Vec, present: u16, n_types: u8, n_key_syms: u8, n_key_actions: u8, total_actions: u16, total_key_behaviors: u8, virtual_mods: u16, total_key_explicit: u8, total_mod_map_keys: u8, total_v_mod_map_keys: u8) { - let _ = present; - let _ = n_types; - let _ = n_key_syms; - let _ = n_key_actions; - let _ = total_actions; - let _ = total_key_behaviors; - let _ = virtual_mods; - let _ = total_key_explicit; - let _ = total_mod_map_keys; - let _ = total_v_mod_map_keys; + assert_eq!(self.switch_expr(), u16::from(present), "switch `map` has an inconsistent discriminant"); if let Some(ref types_rtrn) = self.types_rtrn { assert_eq!(types_rtrn.len(), usize::try_from(n_types).unwrap(), "`types_rtrn` has an incorrect length"); types_rtrn.serialize_into(bytes); @@ -8327,16 +8316,7 @@ impl SetMapAux { result } fn serialize_into(&self, bytes: &mut Vec, present: u16, n_types: u8, n_key_syms: u8, n_key_actions: u8, total_actions: u16, total_key_behaviors: u8, virtual_mods: u16, total_key_explicit: u8, total_mod_map_keys: u8, total_v_mod_map_keys: u8) { - let _ = present; - let _ = n_types; - let _ = n_key_syms; - let _ = n_key_actions; - let _ = total_actions; - let _ = total_key_behaviors; - let _ = virtual_mods; - let _ = total_key_explicit; - let _ = total_mod_map_keys; - let _ = total_v_mod_map_keys; + assert_eq!(self.switch_expr(), u16::from(present), "switch `values` has an inconsistent discriminant"); if let Some(ref types) = self.types { assert_eq!(types.len(), usize::try_from(n_types).unwrap(), "`types` has an incorrect length"); types.serialize_into(bytes); @@ -9878,14 +9858,7 @@ impl GetNamesValueList { result } fn serialize_into(&self, bytes: &mut Vec, which: u32, n_types: u8, indicators: u32, virtual_mods: u16, group_names: u8, n_keys: u8, n_key_aliases: u8, n_radio_groups: u8) { - let _ = which; - let _ = n_types; - let _ = indicators; - let _ = virtual_mods; - let _ = group_names; - let _ = n_keys; - let _ = n_key_aliases; - let _ = n_radio_groups; + assert_eq!(self.switch_expr(), u32::from(which), "switch `value_list` has an inconsistent discriminant"); if let Some(keycodes_name) = self.keycodes_name { keycodes_name.serialize_into(bytes); } @@ -10251,14 +10224,7 @@ impl SetNamesAux { result } fn serialize_into(&self, bytes: &mut Vec, which: u32, n_types: u8, indicators: u32, virtual_mods: u16, group_names: u8, n_keys: u8, n_key_aliases: u8, n_radio_groups: u8) { - let _ = which; - let _ = n_types; - let _ = indicators; - let _ = virtual_mods; - let _ = group_names; - let _ = n_keys; - let _ = n_key_aliases; - let _ = n_radio_groups; + assert_eq!(self.switch_expr(), u32::from(which), "switch `values` has an inconsistent discriminant"); if let Some(keycodes_name) = self.keycodes_name { keycodes_name.serialize_into(bytes); } @@ -11242,16 +11208,7 @@ impl GetKbdByNameRepliesTypesMap { result } fn serialize_into(&self, bytes: &mut Vec, present: u16, n_types: u8, n_key_syms: u8, n_key_actions: u8, total_actions: u16, total_key_behaviors: u8, virtual_mods: u16, total_key_explicit: u8, total_mod_map_keys: u8, total_v_mod_map_keys: u8) { - let _ = present; - let _ = n_types; - let _ = n_key_syms; - let _ = n_key_actions; - let _ = total_actions; - let _ = total_key_behaviors; - let _ = virtual_mods; - let _ = total_key_explicit; - let _ = total_mod_map_keys; - let _ = total_v_mod_map_keys; + assert_eq!(self.switch_expr(), u16::from(present), "switch `map` has an inconsistent discriminant"); if let Some(ref types_rtrn) = self.types_rtrn { assert_eq!(types_rtrn.len(), usize::try_from(n_types).unwrap(), "`types_rtrn` has an incorrect length"); types_rtrn.serialize_into(bytes); @@ -11751,14 +11708,7 @@ impl GetKbdByNameRepliesKeyNamesValueList { result } fn serialize_into(&self, bytes: &mut Vec, which: u32, n_types: u8, indicators: u32, virtual_mods: u16, group_names: u8, n_keys: u8, n_key_aliases: u8, n_radio_groups: u8) { - let _ = which; - let _ = n_types; - let _ = indicators; - let _ = virtual_mods; - let _ = group_names; - let _ = n_keys; - let _ = n_key_aliases; - let _ = n_radio_groups; + assert_eq!(self.switch_expr(), u32::from(which), "switch `value_list` has an inconsistent discriminant"); if let Some(keycodes_name) = self.keycodes_name { keycodes_name.serialize_into(bytes); } diff --git a/x11rb-protocol/src/protocol/xproto.rs b/x11rb-protocol/src/protocol/xproto.rs index bdb7e8be..d357606c 100644 --- a/x11rb-protocol/src/protocol/xproto.rs +++ b/x11rb-protocol/src/protocol/xproto.rs @@ -6924,7 +6924,7 @@ impl CreateWindowAux { result } fn serialize_into(&self, bytes: &mut Vec, value_mask: u32) { - let _ = value_mask; + assert_eq!(self.switch_expr(), u32::from(value_mask), "switch `value_list` has an inconsistent discriminant"); if let Some(background_pixmap) = self.background_pixmap { background_pixmap.serialize_into(bytes); } @@ -7477,7 +7477,7 @@ impl ChangeWindowAttributesAux { result } fn serialize_into(&self, bytes: &mut Vec, value_mask: u32) { - let _ = value_mask; + assert_eq!(self.switch_expr(), u32::from(value_mask), "switch `value_list` has an inconsistent discriminant"); if let Some(background_pixmap) = self.background_pixmap { background_pixmap.serialize_into(bytes); } @@ -8935,7 +8935,7 @@ impl ConfigureWindowAux { result } fn serialize_into(&self, bytes: &mut Vec, value_mask: u16) { - let _ = value_mask; + assert_eq!(self.switch_expr(), u16::from(value_mask), "switch `value_list` has an inconsistent discriminant"); if let Some(x) = self.x { x.serialize_into(bytes); } @@ -16537,7 +16537,7 @@ impl CreateGCAux { result } fn serialize_into(&self, bytes: &mut Vec, value_mask: u32) { - let _ = value_mask; + assert_eq!(self.switch_expr(), u32::from(value_mask), "switch `value_list` has an inconsistent discriminant"); if let Some(function) = self.function { u32::from(function).serialize_into(bytes); } @@ -17174,7 +17174,7 @@ impl ChangeGCAux { result } fn serialize_into(&self, bytes: &mut Vec, value_mask: u32) { - let _ = value_mask; + assert_eq!(self.switch_expr(), u32::from(value_mask), "switch `value_list` has an inconsistent discriminant"); if let Some(function) = self.function { u32::from(function).serialize_into(bytes); } @@ -23393,7 +23393,7 @@ impl ChangeKeyboardControlAux { result } fn serialize_into(&self, bytes: &mut Vec, value_mask: u32) { - let _ = value_mask; + assert_eq!(self.switch_expr(), u32::from(value_mask), "switch `value_list` has an inconsistent discriminant"); if let Some(key_click_percent) = self.key_click_percent { key_click_percent.serialize_into(bytes); }