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

support macaddr::MacAddr6 with Pg #3640

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
3 changes: 2 additions & 1 deletion diesel/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ bitflags = { version = "2.0.0", optional = true }
r2d2 = { version = ">= 0.8.2, < 0.9.0", optional = true }
itoa = { version = "1.0.0", optional = true }
time = { version = "0.3.9", optional = true, features = ["macros"] }
macaddr = { version = "1.0.1", optional = true }

[dependencies.diesel_derives]
version = "~2.0.0"
Expand All @@ -47,7 +48,7 @@ quickcheck = "1.0.3"

[features]
default = ["with-deprecated", "32-column-tables"]
extras = ["chrono", "time", "serde_json", "uuid", "network-address", "numeric", "r2d2"]
extras = ["chrono", "time", "serde_json", "uuid", "network-address", "numeric", "r2d2", "macaddr"]
unstable = ["diesel_derives/nightly"]
large-tables = ["32-column-tables"]
huge-tables = ["64-column-tables"]
Expand Down
25 changes: 25 additions & 0 deletions diesel/src/pg/types/mac_addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,21 @@ impl ToSql<MacAddr, Pg> for [u8; 6] {
}
}

#[cfg(all(feature = "macaddr", feature = "postgres_backend"))]
impl FromSql<MacAddr, Pg> for macaddr::MacAddr6 {
fn from_sql(value: PgValue<'_>) -> deserialize::Result<Self> {
<[u8; 6] as FromSql<MacAddr, Pg>>::from_sql(value).map(Into::into)
}
}

#[cfg(all(feature = "macaddr", feature = "postgres_backend"))]
impl ToSql<MacAddr, Pg> for macaddr::MacAddr6 {
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Pg>) -> serialize::Result {
let array_value = self.into_array();
<[u8; 6] as ToSql<MacAddr, Pg>>::to_sql(&array_value, &mut out.reborrow())
}
}

#[test]
fn macaddr_roundtrip() {
use crate::query_builder::bind_collector::ByteWrapper;
Expand All @@ -45,6 +60,16 @@ fn macaddr_roundtrip() {
let mut bytes = Output::test(ByteWrapper(&mut buffer));
let input_address = [0x52, 0x54, 0x00, 0xfb, 0xc6, 0x16];
ToSql::<MacAddr, Pg>::to_sql(&input_address, &mut bytes).unwrap();

let output_address: [u8; 6] = FromSql::from_sql(PgValue::for_test(&buffer)).unwrap();
assert_eq!(input_address, output_address);

#[cfg(feature = "macaddr")]
{
use macaddr::MacAddr6;

let input_address: MacAddr6 = input_address.into();
let output_address: MacAddr6 = FromSql::from_sql(PgValue::for_test(&buffer)).unwrap();
assert_eq!(input_address, output_address);
}
}
14 changes: 14 additions & 0 deletions diesel_tests/tests/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1048,6 +1048,12 @@ fn pg_macaddress_from_sql() {
expected_value,
query_single_value::<MacAddr, [u8; 6]>(query)
);

#[cfg(feature = "macaddr")]
assert_eq!(
expected_value,
query_single_value::<MacAddr, macaddr::MacAddr6>(query)
);
}

#[test]
Expand All @@ -1059,6 +1065,14 @@ fn pg_macaddress_to_sql_macaddress() {
expected_value,
value
));

#[cfg(feature = "macaddr")]
{
assert!(query_to_sql_equality::<MacAddr, macaddr::MacAddr6>(
expected_value,
macaddr::MacAddr6::new(0x08, 0x00, 0x2b, 0x01, 0x02, 0x03)
));
}
}

#[test]
Expand Down