Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hex serde #278

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft

hex serde #278

wants to merge 1 commit into from

Conversation

burrbull
Copy link
Member

No description provided.

@burrbull burrbull requested a review from a team as a code owner November 18, 2024 19:41
@burrbull burrbull marked this pull request as draft November 18, 2024 19:53
Comment on lines +289 to +309
let n = self.0;
let (h4, h3, h2, h1) = (
(n >> 48) & 0xffff,
(n >> 32) & 0xffff,
(n >> 16) & 0xffff,
n & 0xffff,
);
let f = if h4 != 0 {
format!("0x{h4:04x}{h3:04x}{h2:04x}{h1:04x}")
} else if h3 != 0 {
format!("0x{h3:04x}{h2:04x}{h1:04x}")
} else if h2 != 0 {
format!("0x{h2:04x}{h1:04x}")
} else if h1 & 0xff00 != 0 {
format!("0x{h1:04x}")
} else if h1 != 0 {
format!("0x{:02x}", h1 & 0xff)
} else {
"0".to_string()
};
serializer.serialize_str(&f)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this could be replaced with

let hex_length = ((64 - n.leading_zeros() + 3)/4) as usize;
let width = (4 - (hex_length % 4)) % 4 + hex_length;
format!("0x{:0width$x}", n)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Forgot about this. Thanks.

@burrbull
Copy link
Member Author

burrbull commented Nov 19, 2024

But there is bigger issue. It does not do what I want. It creates "strings" instead of hex integers.
value: "0x10" instead of value: 0x10

@Emilgardis
Copy link
Member

But there is bigger issue. It does not do what I want. It creates "strings" instead of hex integers. value: "0x10" instead of value: 0x10

yeah, that's something for the serializer to do, serde doesn't really have a way to signal it in the struct :/

toml_edit supports this if the field is a Document instead of the actual type

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants