Trait stdto_core::ToHex

source ·
pub trait ToHex: AsBytes {
Show 24 methods fn try_to_hex_into_with_mode(
        &self,
        writer: impl Write,
        mode: HexMode
    ) -> Result<()> { ... } fn try_to_hex_with_mode(&self, mode: HexMode) -> Result<String> { ... } fn try_from_hex(hex: impl AsBytes) -> Result<Vec<u8>> { ... } fn try_from_hex_from(reader: impl Read) -> Result<Vec<u8>> { ... } fn try_copy_from_hex(&mut self, hex: impl AsBytes) -> Result<usize>
    where
        Self: AsMut<[u8]>
, { ... } fn try_to_hex(&self) -> Result<String> { ... } fn try_to_upper_hex(&self) -> Result<String> { ... } fn try_to_hex_with_0x(&self) -> Result<String> { ... } fn try_to_upper_hex_with_0x(&self) -> Result<String> { ... } fn to_hex(&self) -> String { ... } fn to_upper_hex(&self) -> String { ... } fn to_hex_with_0x(&self) -> String { ... } fn to_upper_hex_with_0x(&self) -> String { ... } fn try_to_hex_into(&self, writer: impl Write) -> Result<()> { ... } fn try_to_upper_hex_into(&self, writer: impl Write) -> Result<()> { ... } fn try_to_hex_into_with_0x(&self, writer: impl Write) -> Result<()> { ... } fn try_to_upper_hex_into_with_0x(&self, writer: impl Write) -> Result<()> { ... } fn to_hex_into(&self, writer: impl Write) { ... } fn to_upper_hex_into(&self, writer: impl Write) { ... } fn to_hex_into_with_0x(&self, writer: impl Write) { ... } fn to_upper_hex_into_with_0x(&self, writer: impl Write) { ... } fn from_hex(hex: impl AsBytes) -> Vec<u8> { ... } fn from_hex_from(reader: impl Read) -> Vec<u8> { ... } fn copy_from_hex(&mut self, hex: impl AsBytes) -> usize
    where
        Self: AsMut<[u8]>
, { ... }
}
Expand description

Provided Methods§

Examples found in repository?
src/traits.rs (line 785)
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
    fn try_to_hex_with_mode(&self, mode: HexMode) -> Result<String> {
        let mut hex = String::with_capacity(
            self.as_byte_slice().len() * 2 + if mode.has_0x() { 2 } else { 0 }, //
        );
        self.try_to_hex_into_with_mode(&mut hex, mode)?;
        Ok(hex)
    }
    #[inline]
    fn try_from_hex(hex: impl AsBytes) -> Result<Vec<u8>> {
        let hex = hex.as_byte_slice();
        let hex = if hex.starts_with(&[b'0', b'x']) {
            &hex[2..]
        } else {
            hex
        };
        if hex.len() % 2 != 0 {
            return Err(Error::OddLength);
        }
        let mut bytes = Vec::with_capacity(hex.len() / 2);
        for i in (0..hex.len()).step_by(2) {
            let s = std::str::from_utf8(&hex[i..i + 2])?;
            let byte = u8::from_str_radix(s, 16)?;
            bytes.push(byte);
        }
        Ok(bytes)
    }
    #[inline]
    fn try_from_hex_from(mut reader: impl io::Read) -> Result<Vec<u8>> {
        let mut double = [0u8; 2];
        reader.read_exact(&mut double)?;
        let mut v = Vec::new();
        let mut take_into_v = |double: &mut [u8; 2]| -> Result<()> {
            let ch = std::str::from_utf8(double)?;
            let byte = u8::from_str_radix(ch, 16)?;
            v.push(byte);
            double[0] = 0;
            double[1] = 0;
            Ok(())
        };
        if double != [b'0', b'x'] {
            take_into_v(&mut double)?;
        }
        loop {
            if let Err(e) = reader.read_exact(&mut double) {
                match e.kind() {
                    io::ErrorKind::UnexpectedEof => {
                        if double[0] == 0 {
                            break;
                        }
                        return Err(Error::OddLength);
                    }
                    _ => return Err(Error::Io(e)),
                }
            }
            take_into_v(&mut double)?;
        }
        Ok(v)
    }
    #[inline]
    fn try_copy_from_hex(&mut self, hex: impl AsBytes) -> Result<usize>
    where
        Self: AsMut<[u8]>,
    {
        let hex = hex.as_byte_slice();
        let hex = if hex.starts_with(&[b'0', b'x']) {
            &hex[2..]
        } else {
            hex
        };
        if hex.len() % 2 != 0 {
            return Err(Error::OddLength);
        }
        let hex_bytes_len = hex.len() / 2;
        let bytes = self.as_mut();
        if hex_bytes_len > bytes.len() {
            return Err(Error::OutOfBounds(bytes.len(), hex_bytes_len));
        }
        for i in (0..hex.len()).step_by(2) {
            let s = std::str::from_utf8(&hex[i..i + 2])?;
            let byte = u8::from_str_radix(s, 16)?;
            bytes[i / 2] = byte;
        }
        Ok(hex_bytes_len)
    }

    #[inline]
    fn try_to_hex(&self) -> Result<String> {
        self.try_to_hex_with_mode(HexMode::Lower)
    }
    #[inline]
    fn try_to_upper_hex(&self) -> Result<String> {
        self.try_to_hex_with_mode(HexMode::Upper)
    }
    #[inline]
    fn try_to_hex_with_0x(&self) -> Result<String> {
        self.try_to_hex_with_mode(HexMode::Lower0x)
    }
    #[inline]
    fn try_to_upper_hex_with_0x(&self) -> Result<String> {
        self.try_to_hex_with_mode(HexMode::Upper0x)
    }
    #[inline]
    fn to_hex(&self) -> String {
        self.try_to_hex().unwrap()
    }
    #[inline]
    fn to_upper_hex(&self) -> String {
        self.try_to_upper_hex().unwrap()
    }
    #[inline]
    fn to_hex_with_0x(&self) -> String {
        self.try_to_hex_with_0x().unwrap()
    }
    #[inline]
    fn to_upper_hex_with_0x(&self) -> String {
        self.try_to_upper_hex_with_0x().unwrap()
    }

    #[inline]
    fn try_to_hex_into(&self, writer: impl fmt::Write) -> Result<()> {
        self.try_to_hex_into_with_mode(writer, HexMode::Lower)
    }
    #[inline]
    fn try_to_upper_hex_into(&self, writer: impl fmt::Write) -> Result<()> {
        self.try_to_hex_into_with_mode(writer, HexMode::Upper)
    }
    #[inline]
    fn try_to_hex_into_with_0x(&self, writer: impl fmt::Write) -> Result<()> {
        self.try_to_hex_into_with_mode(writer, HexMode::Lower0x)
    }
    #[inline]
    fn try_to_upper_hex_into_with_0x(&self, writer: impl fmt::Write) -> Result<()> {
        self.try_to_hex_into_with_mode(writer, HexMode::Upper0x)
    }
Examples found in repository?
src/traits.rs (line 868)
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
    fn try_to_hex(&self) -> Result<String> {
        self.try_to_hex_with_mode(HexMode::Lower)
    }
    #[inline]
    fn try_to_upper_hex(&self) -> Result<String> {
        self.try_to_hex_with_mode(HexMode::Upper)
    }
    #[inline]
    fn try_to_hex_with_0x(&self) -> Result<String> {
        self.try_to_hex_with_mode(HexMode::Lower0x)
    }
    #[inline]
    fn try_to_upper_hex_with_0x(&self) -> Result<String> {
        self.try_to_hex_with_mode(HexMode::Upper0x)
    }
Examples found in repository?
src/traits.rs (line 934)
933
934
935
    fn from_hex(hex: impl AsBytes) -> Vec<u8> {
        Self::try_from_hex(hex).unwrap()
    }
Examples found in repository?
src/traits.rs (line 938)
937
938
939
    fn from_hex_from(reader: impl io::Read) -> Vec<u8> {
        Self::try_from_hex_from(reader).unwrap()
    }
Examples found in repository?
src/traits.rs (line 945)
941
942
943
944
945
946
    fn copy_from_hex(&mut self, hex: impl AsBytes) -> usize
    where
        Self: AsMut<[u8]>,
    {
        self.try_copy_from_hex(hex).unwrap()
    }
Examples found in repository?
src/traits.rs (line 884)
883
884
885
    fn to_hex(&self) -> String {
        self.try_to_hex().unwrap()
    }
Examples found in repository?
src/traits.rs (line 888)
887
888
889
    fn to_upper_hex(&self) -> String {
        self.try_to_upper_hex().unwrap()
    }
Examples found in repository?
src/traits.rs (line 892)
891
892
893
    fn to_hex_with_0x(&self) -> String {
        self.try_to_hex_with_0x().unwrap()
    }
Examples found in repository?
src/traits.rs (line 896)
895
896
897
    fn to_upper_hex_with_0x(&self) -> String {
        self.try_to_upper_hex_with_0x().unwrap()
    }
Examples found in repository?
src/traits.rs (line 917)
916
917
918
    fn to_hex_into(&self, writer: impl fmt::Write) {
        self.try_to_hex_into(writer).unwrap()
    }
Examples found in repository?
src/traits.rs (line 921)
920
921
922
    fn to_upper_hex_into(&self, writer: impl fmt::Write) {
        self.try_to_upper_hex_into(writer).unwrap()
    }
Examples found in repository?
src/traits.rs (line 925)
924
925
926
    fn to_hex_into_with_0x(&self, writer: impl fmt::Write) {
        self.try_to_hex_into_with_0x(writer).unwrap()
    }
Examples found in repository?
src/traits.rs (line 929)
928
929
930
    fn to_upper_hex_into_with_0x(&self, writer: impl fmt::Write) {
        self.try_to_upper_hex_into_with_0x(writer).unwrap()
    }

Implementors§

implement ToHex for impl AsBytes