Skip to content

Commit 37cc6cf

Browse files
Merge pull request #490 from ijackson/deser-string-unreachable
deserialize: strings: Introduce Unreachable type
2 parents ba72921 + 9a18f60 commit 37cc6cf

File tree

1 file changed

+32
-29
lines changed

1 file changed

+32
-29
lines changed

src/ser.rs

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ enum SerKey {
1919
Seq(usize),
2020
}
2121

22+
/// An uninhabited type: no values like this can ever exist!
23+
pub enum Unreachable {}
24+
2225
/// Serializer for numbered sequences
2326
///
2427
/// This wrapper is present when we are outputting a sequence (numbered indices).
@@ -427,13 +430,13 @@ macro_rules! string_serialize_via_display { { $method:ident, $type:ty } => {
427430
impl ser::Serializer for StringKeySerializer {
428431
type Ok = String;
429432
type Error = ConfigError;
430-
type SerializeSeq = Self;
431-
type SerializeTuple = Self;
432-
type SerializeTupleStruct = Self;
433-
type SerializeTupleVariant = Self;
434-
type SerializeMap = Self;
435-
type SerializeStruct = Self;
436-
type SerializeStructVariant = Self;
433+
type SerializeSeq = Unreachable;
434+
type SerializeTuple = Unreachable;
435+
type SerializeTupleStruct = Unreachable;
436+
type SerializeTupleVariant = Unreachable;
437+
type SerializeMap = Unreachable;
438+
type SerializeStruct = Unreachable;
439+
type SerializeStructVariant = Unreachable;
437440

438441
string_serialize_via_display!(serialize_bool, bool);
439442
string_serialize_via_display!(serialize_i8, i8);
@@ -560,122 +563,122 @@ impl ser::Serializer for StringKeySerializer {
560563
}
561564
}
562565

563-
impl ser::SerializeSeq for StringKeySerializer {
566+
impl ser::SerializeSeq for Unreachable {
564567
type Ok = String;
565568
type Error = ConfigError;
566569

567570
fn serialize_element<T>(&mut self, _value: &T) -> Result<()>
568571
where
569572
T: ?Sized + ser::Serialize,
570573
{
571-
unreachable!()
574+
match *self {}
572575
}
573576

574577
fn end(self) -> Result<Self::Ok> {
575-
unreachable!()
578+
match self {}
576579
}
577580
}
578581

579-
impl ser::SerializeTuple for StringKeySerializer {
582+
impl ser::SerializeTuple for Unreachable {
580583
type Ok = String;
581584
type Error = ConfigError;
582585

583586
fn serialize_element<T>(&mut self, _value: &T) -> Result<()>
584587
where
585588
T: ?Sized + ser::Serialize,
586589
{
587-
unreachable!()
590+
match *self {}
588591
}
589592

590593
fn end(self) -> Result<Self::Ok> {
591-
unreachable!()
594+
match self {}
592595
}
593596
}
594597

595-
impl ser::SerializeTupleStruct for StringKeySerializer {
598+
impl ser::SerializeTupleStruct for Unreachable {
596599
type Ok = String;
597600
type Error = ConfigError;
598601

599602
fn serialize_field<T>(&mut self, _value: &T) -> Result<()>
600603
where
601604
T: ?Sized + ser::Serialize,
602605
{
603-
unreachable!()
606+
match *self {}
604607
}
605608

606609
fn end(self) -> Result<Self::Ok> {
607-
unreachable!()
610+
match self {}
608611
}
609612
}
610613

611-
impl ser::SerializeTupleVariant for StringKeySerializer {
614+
impl ser::SerializeTupleVariant for Unreachable {
612615
type Ok = String;
613616
type Error = ConfigError;
614617

615618
fn serialize_field<T>(&mut self, _value: &T) -> Result<()>
616619
where
617620
T: ?Sized + ser::Serialize,
618621
{
619-
unreachable!()
622+
match *self {}
620623
}
621624

622625
fn end(self) -> Result<Self::Ok> {
623-
unreachable!()
626+
match self {}
624627
}
625628
}
626629

627-
impl ser::SerializeMap for StringKeySerializer {
630+
impl ser::SerializeMap for Unreachable {
628631
type Ok = String;
629632
type Error = ConfigError;
630633

631634
fn serialize_key<T>(&mut self, _key: &T) -> Result<()>
632635
where
633636
T: ?Sized + ser::Serialize,
634637
{
635-
unreachable!()
638+
match *self {}
636639
}
637640

638641
fn serialize_value<T>(&mut self, _value: &T) -> Result<()>
639642
where
640643
T: ?Sized + ser::Serialize,
641644
{
642-
unreachable!()
645+
match *self {}
643646
}
644647

645648
fn end(self) -> Result<Self::Ok> {
646-
unreachable!()
649+
match self {}
647650
}
648651
}
649652

650-
impl ser::SerializeStruct for StringKeySerializer {
653+
impl ser::SerializeStruct for Unreachable {
651654
type Ok = String;
652655
type Error = ConfigError;
653656

654657
fn serialize_field<T>(&mut self, _key: &'static str, _value: &T) -> Result<()>
655658
where
656659
T: ?Sized + ser::Serialize,
657660
{
658-
unreachable!()
661+
match *self {}
659662
}
660663

661664
fn end(self) -> Result<Self::Ok> {
662-
unreachable!()
665+
match self {}
663666
}
664667
}
665668

666-
impl ser::SerializeStructVariant for StringKeySerializer {
669+
impl ser::SerializeStructVariant for Unreachable {
667670
type Ok = String;
668671
type Error = ConfigError;
669672

670673
fn serialize_field<T>(&mut self, _key: &'static str, _value: &T) -> Result<()>
671674
where
672675
T: ?Sized + ser::Serialize,
673676
{
674-
unreachable!()
677+
match *self {}
675678
}
676679

677680
fn end(self) -> Result<Self::Ok> {
678-
unreachable!()
681+
match self {}
679682
}
680683
}
681684

0 commit comments

Comments
 (0)