From 57ac163a06b7f4b00d9cfd56d0ab6ee6a49adb56 Mon Sep 17 00:00:00 2001 From: hozan23 Date: Wed, 29 Nov 2023 00:32:19 +0300 Subject: net: use `bincode` derive feature to implement Encode and Deconde for Addr --- net/Cargo.toml | 2 +- net/src/endpoint.rs | 39 ++------------------------------------- 2 files changed, 3 insertions(+), 38 deletions(-) diff --git a/net/Cargo.toml b/net/Cargo.toml index 863a250..d2cb884 100644 --- a/net/Cargo.toml +++ b/net/Cargo.toml @@ -11,7 +11,7 @@ karyons_core.workspace = true smol = "1.3.0" async-trait = "0.1.73" log = "0.4.20" -bincode = "2.0.0-rc.3" +bincode = { version="2.0.0-rc.3", features = ["derive"]} thiserror = "1.0.47" url = "2.4.1" async-rustls = { version = "0.4.1", features = ["dangerous_configuration"] } 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( - &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( - decoder: &mut D, - ) -> std::result::Result { - 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::(), - }), - } - } -} -impl_borrow_decode!(Addr); - impl TryFrom for IpAddr { type Error = Error; fn try_from(addr: Addr) -> std::result::Result { -- cgit v1.2.3