Skip to content

Commit e46e933

Browse files
committed
serialize: Read/emit tuples with {read,emit}_tuple
This commit moves from {read,emit}_seq for tuples to {read,emit}_tuple, as well as providing a generalized macro for generating these implementations from one invocation. Closes #13086
1 parent e233a43 commit e46e933

File tree

1 file changed

+30
-140
lines changed

1 file changed

+30
-140
lines changed

src/libserialize/serialize.rs

+30-140
Original file line numberDiff line numberDiff line change
@@ -477,153 +477,43 @@ impl<D:Decoder,T:Decodable<D>> Decodable<D> for Option<T> {
477477
}
478478
}
479479

480-
impl<S:Encoder,T0:Encodable<S>,T1:Encodable<S>> Encodable<S> for (T0, T1) {
481-
fn encode(&self, s: &mut S) {
482-
match *self {
483-
(ref t0, ref t1) => {
484-
s.emit_seq(2, |s| {
485-
s.emit_seq_elt(0, |s| t0.encode(s));
486-
s.emit_seq_elt(1, |s| t1.encode(s));
487-
})
488-
}
489-
}
490-
}
491-
}
492-
493-
impl<D:Decoder,T0:Decodable<D>,T1:Decodable<D>> Decodable<D> for (T0, T1) {
494-
fn decode(d: &mut D) -> (T0, T1) {
495-
d.read_seq(|d, len| {
496-
assert_eq!(len, 2);
497-
(
498-
d.read_seq_elt(0, |d| Decodable::decode(d)),
499-
d.read_seq_elt(1, |d| Decodable::decode(d))
500-
)
501-
})
502-
}
503-
}
504-
505-
impl<
506-
S: Encoder,
507-
T0: Encodable<S>,
508-
T1: Encodable<S>,
509-
T2: Encodable<S>
510-
> Encodable<S> for (T0, T1, T2) {
511-
fn encode(&self, s: &mut S) {
512-
match *self {
513-
(ref t0, ref t1, ref t2) => {
514-
s.emit_seq(3, |s| {
515-
s.emit_seq_elt(0, |s| t0.encode(s));
516-
s.emit_seq_elt(1, |s| t1.encode(s));
517-
s.emit_seq_elt(2, |s| t2.encode(s));
518-
})
519-
}
520-
}
521-
}
522-
}
523-
524-
impl<
525-
D: Decoder,
526-
T0: Decodable<D>,
527-
T1: Decodable<D>,
528-
T2: Decodable<D>
529-
> Decodable<D> for (T0, T1, T2) {
530-
fn decode(d: &mut D) -> (T0, T1, T2) {
531-
d.read_seq(|d, len| {
532-
assert_eq!(len, 3);
533-
(
534-
d.read_seq_elt(0, |d| Decodable::decode(d)),
535-
d.read_seq_elt(1, |d| Decodable::decode(d)),
536-
d.read_seq_elt(2, |d| Decodable::decode(d))
537-
)
538-
})
539-
}
540-
}
541-
542-
impl<
543-
S: Encoder,
544-
T0: Encodable<S>,
545-
T1: Encodable<S>,
546-
T2: Encodable<S>,
547-
T3: Encodable<S>
548-
> Encodable<S> for (T0, T1, T2, T3) {
549-
fn encode(&self, s: &mut S) {
550-
match *self {
551-
(ref t0, ref t1, ref t2, ref t3) => {
552-
s.emit_seq(4, |s| {
553-
s.emit_seq_elt(0, |s| t0.encode(s));
554-
s.emit_seq_elt(1, |s| t1.encode(s));
555-
s.emit_seq_elt(2, |s| t2.encode(s));
556-
s.emit_seq_elt(3, |s| t3.encode(s));
480+
macro_rules! peel(($name:ident, $($other:ident,)*) => (tuple!($($other,)*)))
481+
482+
macro_rules! tuple (
483+
() => ();
484+
( $($name:ident,)+ ) => (
485+
impl<D:Decoder,$($name:Decodable<D>),*> Decodable<D> for ($($name,)*) {
486+
#[allow(uppercase_variables)]
487+
fn decode(d: &mut D) -> ($($name,)*) {
488+
d.read_tuple(|d, amt| {
489+
let mut i = 0;
490+
let ret = ($(d.read_tuple_arg({ i+=1; i-1 }, |d| -> $name {
491+
Decodable::decode(d)
492+
}),)*);
493+
assert!(amt == i,
494+
"expected tuple of length `{}`, found tuple \
495+
of length `{}`", i, amt);
496+
return ret;
557497
})
558498
}
559499
}
560-
}
561-
}
562-
563-
impl<
564-
D: Decoder,
565-
T0: Decodable<D>,
566-
T1: Decodable<D>,
567-
T2: Decodable<D>,
568-
T3: Decodable<D>
569-
> Decodable<D> for (T0, T1, T2, T3) {
570-
fn decode(d: &mut D) -> (T0, T1, T2, T3) {
571-
d.read_seq(|d, len| {
572-
assert_eq!(len, 4);
573-
(
574-
d.read_seq_elt(0, |d| Decodable::decode(d)),
575-
d.read_seq_elt(1, |d| Decodable::decode(d)),
576-
d.read_seq_elt(2, |d| Decodable::decode(d)),
577-
d.read_seq_elt(3, |d| Decodable::decode(d))
578-
)
579-
})
580-
}
581-
}
582-
583-
impl<
584-
S: Encoder,
585-
T0: Encodable<S>,
586-
T1: Encodable<S>,
587-
T2: Encodable<S>,
588-
T3: Encodable<S>,
589-
T4: Encodable<S>
590-
> Encodable<S> for (T0, T1, T2, T3, T4) {
591-
fn encode(&self, s: &mut S) {
592-
match *self {
593-
(ref t0, ref t1, ref t2, ref t3, ref t4) => {
594-
s.emit_seq(5, |s| {
595-
s.emit_seq_elt(0, |s| t0.encode(s));
596-
s.emit_seq_elt(1, |s| t1.encode(s));
597-
s.emit_seq_elt(2, |s| t2.encode(s));
598-
s.emit_seq_elt(3, |s| t3.encode(s));
599-
s.emit_seq_elt(4, |s| t4.encode(s));
500+
impl<S:Encoder,$($name:Encodable<S>),*> Encodable<S> for ($($name,)*) {
501+
#[allow(uppercase_variables)]
502+
fn encode(&self, s: &mut S) {
503+
let ($(ref $name,)*) = *self;
504+
let mut n = 0;
505+
$(let $name = $name; n += 1;)*
506+
s.emit_tuple(n, |s| {
507+
let mut i = 0;
508+
$(s.emit_seq_elt({ i+=1; i-1 }, |s| $name.encode(s));)*
600509
})
601510
}
602511
}
603-
}
604-
}
512+
peel!($($name,)*)
513+
)
514+
)
605515

606-
impl<
607-
D: Decoder,
608-
T0: Decodable<D>,
609-
T1: Decodable<D>,
610-
T2: Decodable<D>,
611-
T3: Decodable<D>,
612-
T4: Decodable<D>
613-
> Decodable<D> for (T0, T1, T2, T3, T4) {
614-
fn decode(d: &mut D) -> (T0, T1, T2, T3, T4) {
615-
d.read_seq(|d, len| {
616-
assert_eq!(len, 5);
617-
(
618-
d.read_seq_elt(0, |d| Decodable::decode(d)),
619-
d.read_seq_elt(1, |d| Decodable::decode(d)),
620-
d.read_seq_elt(2, |d| Decodable::decode(d)),
621-
d.read_seq_elt(3, |d| Decodable::decode(d)),
622-
d.read_seq_elt(4, |d| Decodable::decode(d))
623-
)
624-
})
625-
}
626-
}
516+
tuple! { T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, }
627517

628518
impl<E: Encoder> Encodable<E> for path::posix::Path {
629519
fn encode(&self, e: &mut E) {

0 commit comments

Comments
 (0)