mirror of
https://gitlab.cern.ch/wotsubo/endcap-sl-software-ri-generator.git
synced 2025-06-08 05:55:44 +09:00
update(generator): use u8 and u16 for register value when possible
This commit is contained in:
parent
5ef9387d33
commit
1b0be30908
3 changed files with 41 additions and 16 deletions
|
@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||||
- Improve error message from syn in generator. !15
|
- Improve error message from syn in generator. !15
|
||||||
- Removed top level module (register_interface), and now it can have arbitrary name.
|
- Removed top level module (register_interface), and now it can have arbitrary name.
|
||||||
- Use full path `RegisterSpec`s to avoid unused `use`
|
- Use full path `RegisterSpec`s to avoid unused `use`
|
||||||
|
- Use `u8` and `u16` for `T` of registers when the mask is 0xff or 0xffff, instead of defining new tuple struct.
|
||||||
|
|
||||||
## [0.3.1] - 2025-04-11
|
## [0.3.1] - 2025-04-11
|
||||||
|
|
||||||
|
|
|
@ -82,6 +82,21 @@ mod util {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Derive appropriate rust type for right aligned `mask`ed value.
|
||||||
|
pub(super) fn from_exact_mask(mask: u32) -> Option<RustUxTypes> {
|
||||||
|
if mask.trailing_zeros() != 0 {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
match 32 - mask.leading_zeros() {
|
||||||
|
0 => panic!("mask cannot be 0"),
|
||||||
|
1 => Some(RustUxTypes::Bool),
|
||||||
|
8 => Some(RustUxTypes::U8),
|
||||||
|
16 => Some(RustUxTypes::U16),
|
||||||
|
32 => Some(RustUxTypes::U32),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub(super) fn to_rust_type_token(&self) -> proc_macro2::Ident {
|
pub(super) fn to_rust_type_token(&self) -> proc_macro2::Ident {
|
||||||
match self {
|
match self {
|
||||||
RustUxTypes::Bool => parse_to_ident("bool").unwrap(),
|
RustUxTypes::Bool => parse_to_ident("bool").unwrap(),
|
||||||
|
|
|
@ -71,26 +71,35 @@ fn reg_type_def_masked(
|
||||||
let x: RustUxTypes = basetype.into();
|
let x: RustUxTypes = basetype.into();
|
||||||
x.to_rust_type_token()
|
x.to_rust_type_token()
|
||||||
};
|
};
|
||||||
|
let type_t = RustUxTypes::from_exact_mask(mask);
|
||||||
|
|
||||||
let out = quote! {
|
// u8, u16 already implements Into and TryFrom with u32
|
||||||
#[derive(Debug, Clone, Copy, Default)]
|
// but bool doen't implement TryFrom<u32>, so it cannot be used as `T` here
|
||||||
pub struct #upper_camel_name(pub #type_ux);
|
match type_t {
|
||||||
impl TryFrom<#type_ux> for #upper_camel_name {
|
None | Some(RustUxTypes::Bool) => {
|
||||||
type Error = crate::register_spec::DataConversionError<#type_ux, Self>;
|
let out = quote! {
|
||||||
|
#[derive(Debug, Clone, Copy, Default)]
|
||||||
|
pub struct #upper_camel_name(pub #type_ux);
|
||||||
|
impl TryFrom<#type_ux> for #upper_camel_name {
|
||||||
|
type Error = crate::register_spec::DataConversionError<#type_ux, Self>;
|
||||||
|
|
||||||
fn try_from(value: #type_ux) -> Result<Self, Self::Error> {
|
fn try_from(value: #type_ux) -> Result<Self, Self::Error> {
|
||||||
Ok(Self(value & #mask))
|
Ok(Self(value & #mask))
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
impl From<#upper_camel_name> for #type_ux {
|
||||||
|
fn from(value: #upper_camel_name) -> Self {
|
||||||
|
value.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
(out, upper_camel_name.clone(), type_ux)
|
||||||
}
|
}
|
||||||
impl From<#upper_camel_name> for #type_ux {
|
Some(type_t) => {
|
||||||
fn from(value: #upper_camel_name) -> Self {
|
let out = quote! {};
|
||||||
value.0
|
(out, type_t.to_rust_type_token(), type_ux)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
};
|
|
||||||
|
|
||||||
(out, upper_camel_name.clone(), type_ux)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Where `T` has fields.
|
/// Where `T` has fields.
|
||||||
|
|
Loading…
Add table
Reference in a new issue