Skip to content

Commit 398771b

Browse files
committed
avoid all the FromLeBytes boilerplate, pass iNN::from_le_bytes as a parameter to the decode_int function
1 parent b3dd6de commit 398771b

File tree

1 file changed

+10
-35
lines changed
  • sqlx-core/src/mssql/types

1 file changed

+10
-35
lines changed

sqlx-core/src/mssql/types/int.rs

Lines changed: 10 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -26,37 +26,12 @@ impl Encode<'_, Mssql> for i8 {
2626
}
2727
}
2828

29-
trait FromLeBytes<const N: usize> {
30-
fn from_le_bytes(bytes: [u8; N]) -> Self;
31-
}
32-
33-
impl FromLeBytes<1> for i8 {
34-
fn from_le_bytes(bytes: [u8; 1]) -> Self {
35-
i8::from_le_bytes(bytes)
36-
}
37-
}
38-
39-
impl FromLeBytes<2> for i16 {
40-
fn from_le_bytes(bytes: [u8; 2]) -> Self {
41-
i16::from_le_bytes(bytes)
42-
}
43-
}
44-
45-
impl FromLeBytes<4> for i32 {
46-
fn from_le_bytes(bytes: [u8; 4]) -> Self {
47-
i32::from_le_bytes(bytes)
48-
}
49-
}
50-
51-
impl FromLeBytes<8> for i64 {
52-
fn from_le_bytes(bytes: [u8; 8]) -> Self {
53-
i64::from_le_bytes(bytes)
54-
}
55-
}
56-
57-
fn decode_int_direct<T, const N: usize>(value: MssqlValueRef<'_>) -> Result<T, BoxDynError>
29+
fn decode_int_direct<T, const N: usize>(
30+
value: MssqlValueRef<'_>,
31+
from_le_bytes: impl FnOnce([u8; N]) -> T,
32+
) -> Result<T, BoxDynError>
5833
where
59-
T: FromLeBytes<N> + TryFrom<i64>,
34+
T: TryFrom<i64>,
6035
T::Error: std::error::Error + Send + Sync + 'static,
6136
{
6237
let ty = value.type_info.0.ty;
@@ -86,7 +61,7 @@ where
8661

8762
let mut buf = [0u8; N];
8863
buf[..len].copy_from_slice(bytes_val);
89-
Ok(T::from_le_bytes(buf))
64+
Ok(from_le_bytes(buf))
9065
}
9166
DataType::Numeric | DataType::NumericN | DataType::Decimal | DataType::DecimalN => {
9267
let i64_val = decode_numeric(value.as_bytes()?, precision, scale)?;
@@ -104,7 +79,7 @@ where
10479

10580
impl Decode<'_, Mssql> for i8 {
10681
fn decode(value: MssqlValueRef<'_>) -> Result<Self, BoxDynError> {
107-
decode_int_direct::<Self, 1>(value)
82+
decode_int_direct(value, i8::from_le_bytes)
10883
}
10984
}
11085

@@ -131,7 +106,7 @@ impl Encode<'_, Mssql> for i16 {
131106

132107
impl Decode<'_, Mssql> for i16 {
133108
fn decode(value: MssqlValueRef<'_>) -> Result<Self, BoxDynError> {
134-
decode_int_direct::<Self, 2>(value)
109+
decode_int_direct(value, i16::from_le_bytes)
135110
}
136111
}
137112

@@ -155,7 +130,7 @@ impl Encode<'_, Mssql> for i32 {
155130

156131
impl Decode<'_, Mssql> for i32 {
157132
fn decode(value: MssqlValueRef<'_>) -> Result<Self, BoxDynError> {
158-
decode_int_direct::<Self, 4>(value)
133+
decode_int_direct(value, i32::from_le_bytes)
159134
}
160135
}
161136

@@ -190,7 +165,7 @@ impl Encode<'_, Mssql> for i64 {
190165

191166
impl Decode<'_, Mssql> for i64 {
192167
fn decode(value: MssqlValueRef<'_>) -> Result<Self, BoxDynError> {
193-
decode_int_direct::<Self, 8>(value)
168+
decode_int_direct(value, i64::from_le_bytes)
194169
}
195170
}
196171

0 commit comments

Comments
 (0)