Skip to content
This repository was archived by the owner on May 27, 2022. It is now read-only.

Commit 35661cb

Browse files
committed
[serde-reflection] simplify code after rust-lang/rust#68354
1 parent ee146df commit 35661cb

File tree

1 file changed

+47
-83
lines changed

1 file changed

+47
-83
lines changed

serde-reflection/src/format.rs

+47-83
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,10 @@ pub trait FormatHolder {
182182
fn is_unknown(&self) -> bool;
183183
}
184184

185-
fn unification_error<T>(v1: T, v2: T) -> Error
185+
fn unification_error<T1, T2>(v1: T1, v2: T2) -> Error
186186
where
187-
T: std::fmt::Debug,
187+
T1: std::fmt::Debug,
188+
T2: std::fmt::Debug,
188189
{
189190
Error::Incompatible(format!("{:?}", v1), format!("{:?}", v2))
190191
}
@@ -237,18 +238,13 @@ impl FormatHolder for VariantFormat {
237238
Ok(())
238239
}
239240

240-
fn unify(&mut self, mut format: VariantFormat) -> Result<()> {
241-
// Matching `&mut format` instead of `format` because of
242-
// "error[E0009]: cannot bind by-move and by-ref in the same pattern"
243-
// See also https://github.com/rust-lang/rust/issues/68354
244-
// We make it work using std::mem::take (and the Default trait).
245-
match (&mut *self, &mut format) {
246-
(_, Self::Variable(variable2)) => {
241+
fn unify(&mut self, format: VariantFormat) -> Result<()> {
242+
match (self, format) {
243+
(format1, Self::Variable(variable2)) => {
247244
assert!(variable2.borrow().is_none());
248-
*variable2.borrow_mut() = Some(self.clone());
245+
*variable2.borrow_mut() = Some(format1.clone());
249246
}
250-
(Self::Variable(variable1), _) => {
251-
let format2 = std::mem::take(&mut format);
247+
(Self::Variable(variable1), format2) => {
252248
let inner_variable = match variable1.borrow_mut().deref_mut() {
253249
value1 @ None => {
254250
*value1 = Some(format2);
@@ -271,34 +267,26 @@ impl FormatHolder for VariantFormat {
271267
(Self::Unit, Self::Unit) => (),
272268

273269
(Self::NewType(format1), Self::NewType(format2)) => {
274-
let format2 = std::mem::take(format2.as_mut());
275-
format1.as_mut().unify(format2)?;
270+
format1.as_mut().unify(*format2)?;
276271
}
277272

278-
(Self::Tuple(formats1), Self::Tuple(formats2)) => {
279-
if formats1.len() != formats2.len() {
280-
return Err(unification_error(formats1, formats2));
281-
}
282-
let mut formats2 = formats2.iter_mut();
283-
for format1 in formats1 {
284-
let format2 = std::mem::take(formats2.next().unwrap());
273+
(Self::Tuple(formats1), Self::Tuple(formats2)) if formats1.len() == formats2.len() => {
274+
for (format1, format2) in formats1.iter_mut().zip(formats2.into_iter()) {
285275
format1.unify(format2)?;
286276
}
287277
}
288278

289-
(Self::Struct(formats1), Self::Struct(formats2)) => {
290-
if formats1.len() != formats2.len() {
291-
return Err(unification_error(formats1, formats2));
292-
}
293-
let mut formats2 = formats2.iter_mut();
294-
for format1 in formats1 {
295-
let format2 = std::mem::take(formats2.next().unwrap());
279+
(Self::Struct(named_formats1), Self::Struct(named_formats2))
280+
if named_formats1.len() == named_formats2.len() =>
281+
{
282+
for (format1, format2) in named_formats1.iter_mut().zip(named_formats2.into_iter())
283+
{
296284
format1.unify(format2)?;
297285
}
298286
}
299287

300-
_ => {
301-
return Err(unification_error(self, &mut format));
288+
(format1, format2) => {
289+
return Err(unification_error(format1, format2));
302290
}
303291
}
304292
Ok(())
@@ -458,43 +446,34 @@ impl FormatHolder for ContainerFormat {
458446
Ok(())
459447
}
460448

461-
fn unify(&mut self, mut format: ContainerFormat) -> Result<()> {
462-
// Matching `&mut format` instead of `format` because of
463-
// "error[E0009]: cannot bind by-move and by-ref in the same pattern"
464-
match (&mut *self, &mut format) {
449+
fn unify(&mut self, format: ContainerFormat) -> Result<()> {
450+
match (self, format) {
465451
(Self::UnitStruct, Self::UnitStruct) => (),
466452

467453
(Self::NewTypeStruct(format1), Self::NewTypeStruct(format2)) => {
468-
let format2 = std::mem::take(format2.as_mut());
469-
format1.as_mut().unify(format2)?;
454+
format1.as_mut().unify(*format2)?;
470455
}
471456

472-
(Self::TupleStruct(formats1), Self::TupleStruct(formats2)) => {
473-
if formats1.len() != formats2.len() {
474-
return Err(unification_error(self, &mut format));
475-
}
476-
let mut formats2 = formats2.iter_mut();
477-
for format1 in formats1 {
478-
let format2 = std::mem::take(formats2.next().unwrap());
457+
(Self::TupleStruct(formats1), Self::TupleStruct(formats2))
458+
if formats1.len() == formats2.len() =>
459+
{
460+
for (format1, format2) in formats1.iter_mut().zip(formats2.into_iter()) {
479461
format1.unify(format2)?;
480462
}
481463
}
482464

483-
(Self::Struct(named_formats1), Self::Struct(named_formats2)) => {
484-
if named_formats1.len() != named_formats2.len() {
485-
return Err(unification_error(self, &mut format));
486-
}
487-
let mut named_formats2 = named_formats2.iter_mut();
488-
for format1 in named_formats1 {
489-
let format2 = std::mem::take(named_formats2.next().unwrap());
465+
(Self::Struct(named_formats1), Self::Struct(named_formats2))
466+
if named_formats1.len() == named_formats2.len() =>
467+
{
468+
for (format1, format2) in named_formats1.iter_mut().zip(named_formats2.into_iter())
469+
{
490470
format1.unify(format2)?;
491471
}
492472
}
493473

494474
(Self::Enum(variants1), Self::Enum(variants2)) => {
495-
for (index2, variant2) in variants2.iter_mut() {
496-
let variant2 = std::mem::take(variant2);
497-
match variants1.entry(*index2) {
475+
for (index2, variant2) in variants2.into_iter() {
476+
match variants1.entry(index2) {
498477
Entry::Vacant(e) => {
499478
// Note that we do not check for name collisions.
500479
e.insert(variant2);
@@ -506,8 +485,8 @@ impl FormatHolder for ContainerFormat {
506485
}
507486
}
508487

509-
_ => {
510-
return Err(unification_error(self, &mut format));
488+
(format1, format2) => {
489+
return Err(unification_error(format1, format2));
511490
}
512491
}
513492
Ok(())
@@ -616,16 +595,13 @@ impl FormatHolder for Format {
616595

617596
/// Unify the newly "traced" value `format` into the current format.
618597
/// Note that there should be no `TupleArray`s at this point.
619-
fn unify(&mut self, mut format: Format) -> Result<()> {
620-
// Matching `&mut format` instead of `format` because of
621-
// "error[E0009]: cannot bind by-move and by-ref in the same pattern"
622-
match (&mut *self, &mut format) {
623-
(_, Self::Variable(variable2)) => {
598+
fn unify(&mut self, format: Format) -> Result<()> {
599+
match (self, format) {
600+
(format1, Self::Variable(variable2)) => {
624601
assert!(variable2.borrow().is_none());
625-
*variable2.borrow_mut() = Some(self.clone());
602+
*variable2.borrow_mut() = Some(format1.clone());
626603
}
627-
(Self::Variable(variable1), _) => {
628-
let format2 = std::mem::take(&mut format);
604+
(Self::Variable(variable1), format2) => {
629605
let inner_variable = match variable1.borrow_mut().deref_mut() {
630606
value1 @ None => {
631607
*value1 = Some(format2);
@@ -663,25 +639,15 @@ impl FormatHolder for Format {
663639
| (Self::Str, Self::Str)
664640
| (Self::Bytes, Self::Bytes) => (),
665641

666-
(Self::TypeName(name1), Self::TypeName(name2)) => {
667-
if name1 != name2 {
668-
return Err(unification_error(self, &mut format));
669-
}
670-
}
642+
(Self::TypeName(name1), Self::TypeName(name2)) if *name1 == name2 => (),
671643

672644
(Self::Option(format1), Self::Option(format2))
673645
| (Self::Seq(format1), Self::Seq(format2)) => {
674-
let format2 = std::mem::take(format2.as_mut());
675-
format1.as_mut().unify(format2)?;
646+
format1.as_mut().unify(*format2)?;
676647
}
677648

678-
(Self::Tuple(formats1), Self::Tuple(formats2)) => {
679-
if formats1.len() != formats2.len() {
680-
return Err(unification_error(self, &mut format));
681-
}
682-
let mut formats2 = formats2.iter_mut();
683-
for format1 in formats1 {
684-
let format2 = std::mem::take(formats2.next().unwrap());
649+
(Self::Tuple(formats1), Self::Tuple(formats2)) if formats1.len() == formats2.len() => {
650+
for (format1, format2) in formats1.iter_mut().zip(formats2.into_iter()) {
685651
format1.unify(format2)?;
686652
}
687653
}
@@ -696,14 +662,12 @@ impl FormatHolder for Format {
696662
value: value2,
697663
},
698664
) => {
699-
let key2 = std::mem::take(key2.as_mut());
700-
let value2 = std::mem::take(value2.as_mut());
701-
key1.as_mut().unify(key2)?;
702-
value1.as_mut().unify(value2)?;
665+
key1.as_mut().unify(*key2)?;
666+
value1.as_mut().unify(*value2)?;
703667
}
704668

705-
_ => {
706-
return Err(unification_error(self, &mut format));
669+
(format1, format2) => {
670+
return Err(unification_error(format1, format2));
707671
}
708672
}
709673
Ok(())

0 commit comments

Comments
 (0)