aboutsummaryrefslogtreecommitdiff
path: root/net/src
diff options
context:
space:
mode:
authorhozan23 <hozan23@proton.me>2023-11-29 00:32:19 +0300
committerhozan23 <hozan23@proton.me>2023-11-29 00:32:19 +0300
commit57ac163a06b7f4b00d9cfd56d0ab6ee6a49adb56 (patch)
tree12bfe81844601c2e71ef5b086f2fa1ae8785d429 /net/src
parent2b032229e46293af92db798a36793c6b8b97baee (diff)
net: use `bincode` derive feature to implement Encode and Deconde for Addr
Diffstat (limited to 'net/src')
-rw-r--r--net/src/endpoint.rs39
1 files changed, 2 insertions, 37 deletions
diff --git a/net/src/endpoint.rs b/net/src/endpoint.rs
index 720eea3..1a1f975 100644
--- a/net/src/endpoint.rs
+++ b/net/src/endpoint.rs
@@ -5,7 +5,7 @@ use std::{
str::FromStr,
};
-use bincode::{impl_borrow_decode, Decode, Encode};
+use bincode::{Decode, Encode};
use url::Url;
use crate::{Error, Result};
@@ -177,47 +177,12 @@ impl Endpoint {
}
/// Addr defines a type for an address, either IP or domain.
-#[derive(Debug, Clone, PartialEq, Eq, Hash)]
+#[derive(Debug, Clone, PartialEq, Eq, Hash, Encode, Decode)]
pub enum Addr {
Ip(IpAddr),
Domain(String),
}
-impl Encode for Addr {
- fn encode<E: bincode::enc::Encoder>(
- &self,
- encoder: &mut E,
- ) -> std::result::Result<(), bincode::error::EncodeError> {
- match self {
- Addr::Ip(addr) => {
- 0u32.encode(encoder)?;
- addr.encode(encoder)
- }
- Addr::Domain(domain) => {
- 1u32.encode(encoder)?;
- domain.encode(encoder)
- }
- }
- }
-}
-
-impl Decode for Addr {
- fn decode<D: bincode::de::Decoder>(
- decoder: &mut D,
- ) -> std::result::Result<Self, bincode::error::DecodeError> {
- match u32::decode(decoder)? {
- 0 => Ok(Addr::Ip(IpAddr::decode(decoder)?)),
- 1 => Ok(Addr::Domain(String::decode(decoder)?)),
- found => Err(bincode::error::DecodeError::UnexpectedVariant {
- allowed: &bincode::error::AllowedEnumVariants::Range { min: 0, max: 1 },
- found,
- type_name: core::any::type_name::<Addr>(),
- }),
- }
- }
-}
-impl_borrow_decode!(Addr);
-
impl TryFrom<Addr> for IpAddr {
type Error = Error;
fn try_from(addr: Addr) -> std::result::Result<IpAddr, Self::Error> {