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

[TRIVIAL] Format hex encoding with derive_more #3273

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/database/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ license = "MIT OR Apache-2.0"
bigdecimal = { workspace = true }
chrono = { workspace = true, features = ["clock"] }
const_format = "0.2.32"
derive_more = { workspace = true }
futures = { workspace = true }
hex = { workspace = true }
sqlx = { workspace = true }
Expand Down
31 changes: 12 additions & 19 deletions crates/database/src/byte_array.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,19 @@
use {
sqlx::{
encode::IsNull,
error::BoxDynError,
postgres::{PgArgumentBuffer, PgHasArrayType, PgTypeInfo, PgValueFormat, PgValueRef},
Decode,
Encode,
Postgres,
Type,
},
std::fmt::{self, Debug, Formatter},
use sqlx::{
encode::IsNull,
error::BoxDynError,
postgres::{PgArgumentBuffer, PgHasArrayType, PgTypeInfo, PgValueFormat, PgValueRef},
Decode,
Encode,
Postgres,
Type,
};

/// Wrapper type for fixed size byte arrays compatible with sqlx's Postgres
/// implementation.
#[derive(Clone, Copy, Eq, PartialEq, Hash, sqlx::FromRow)]
pub struct ByteArray<const N: usize>(pub [u8; N]);

impl<const N: usize> Debug for ByteArray<N> {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "0x{}", hex::encode(self.0))
}
}
#[derive(Clone, Copy, Eq, PartialEq, Hash, sqlx::FromRow, derive_more::Debug)]
pub struct ByteArray<const N: usize>(
#[debug("0x{}", hex::encode::<&[u8]>(self.0.as_ref()))] pub [u8; N],
);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Originally I wanted to make this PR to change it in all the occurrences, but it cannot be changed for the occurrences which have a generic type (e.g. pub Type<T>(T)).

The reason is that hex::encode() requires the implementation of AsRef<[u8]>. So, theoretically we could do pub Type<T: AsRef<[u8]>(T), but it won't work. The reason is that the macro expands before the compiler acknowledges the trait limitation. This is a limitation in the derive_more crate which does not acknowledge struct-level bounds.

So, in the end this PR only changes one occurrence. I am up to close it if it does not add any benefit.


impl<const N: usize> Default for ByteArray<N> {
fn default() -> Self {
Expand Down
Loading