Skip to content

Commit fa71469

Browse files
skiuscberner
authored andcommittedFeb 27, 2025·
implement Key and Value for (T,)
1 parent 0358e49 commit fa71469

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed
 

‎src/tuple_types.rs

+41
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,46 @@ macro_rules! tuple_impl {
235235
};
236236
}
237237

238+
impl<T: Value> Value for (T,) {
239+
type SelfType<'a>
240+
= (T::SelfType<'a>,)
241+
where
242+
Self: 'a;
243+
type AsBytes<'a>
244+
= T::AsBytes<'a>
245+
where
246+
Self: 'a;
247+
248+
fn fixed_width() -> Option<usize> {
249+
T::fixed_width()
250+
}
251+
252+
fn from_bytes<'a>(data: &'a [u8]) -> Self::SelfType<'a>
253+
where
254+
Self: 'a,
255+
{
256+
(T::from_bytes(data),)
257+
}
258+
259+
fn as_bytes<'a, 'b: 'a>(value: &'a Self::SelfType<'b>) -> Self::AsBytes<'a>
260+
where
261+
Self: 'a,
262+
Self: 'b,
263+
{
264+
T::as_bytes(&value.0)
265+
}
266+
267+
fn type_name() -> TypeName {
268+
TypeName::internal(&format!("({},)", T::type_name().name()))
269+
}
270+
}
271+
272+
impl<T: Key> Key for (T,) {
273+
fn compare(data1: &[u8], data2: &[u8]) -> Ordering {
274+
T::compare(data1, data2)
275+
}
276+
}
277+
238278
tuple_impl! {
239279
T0, t0, 0
240280
| T1, t1, 1
@@ -353,6 +393,7 @@ mod test {
353393
fn width() {
354394
assert!(<(&str, u8)>::fixed_width().is_none());
355395
assert!(<(u16, u8, &str, u128)>::fixed_width().is_none());
396+
assert_eq!(<(u16,)>::fixed_width().unwrap(), 2);
356397
assert_eq!(<(u16, u8)>::fixed_width().unwrap(), 3);
357398
assert_eq!(<(u16, u8, u128)>::fixed_width().unwrap(), 19);
358399
assert_eq!(<(u16, u8, i8, u128)>::fixed_width().unwrap(), 20);

0 commit comments

Comments
 (0)
Please sign in to comment.