Skip to content

Commit

Permalink
chore: small fixes, removed unused code
Browse files Browse the repository at this point in the history
  • Loading branch information
merklefruit committed Aug 26, 2024
1 parent 30675df commit f0cd1e3
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 66 deletions.
6 changes: 0 additions & 6 deletions msg-common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#![cfg_attr(not(test), warn(unused_crate_dependencies))]

use std::{
io,
pin::Pin,
task::{Context, Poll},
time::SystemTime,
Expand Down Expand Up @@ -35,11 +34,6 @@ pub fn async_error<E: std::error::Error + Send + 'static, T>(
Box::pin(async move { Err(e) })
}

/// Creates a new [`io::Error`] with the given message.
pub fn io_error(msg: impl Into<String>) -> io::Error {
io::Error::new(io::ErrorKind::Other, msg.into())
}

#[allow(non_upper_case_globals)]
pub mod constants {
pub const KiB: u32 = 1024;
Expand Down
61 changes: 1 addition & 60 deletions msg-transport/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
#![cfg_attr(not(test), warn(unused_crate_dependencies))]

use futures::{Future, FutureExt};
use msg_common::io_error;
use std::{
fmt::Debug,
hash::Hash,
io,
marker::PhantomData,
net::{SocketAddr, ToSocketAddrs},
net::SocketAddr,
path::PathBuf,
pin::Pin,
task::{Context, Poll},
Expand All @@ -29,64 +28,6 @@ impl Address for SocketAddr {}
/// File system path, used for IPC transport.
impl Address for PathBuf {}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum AddressType {
SocketAddr(SocketAddr),
PathBuf(PathBuf),
}

impl From<SocketAddr> for AddressType {
fn from(addr: SocketAddr) -> Self {
AddressType::SocketAddr(addr)
}
}

impl From<PathBuf> for AddressType {
fn from(path: PathBuf) -> Self {
AddressType::PathBuf(path)
}
}

impl TryFrom<&str> for AddressType {
type Error = io::Error;

fn try_from(value: &str) -> Result<Self, Self::Error> {
let s = value.to_string();
if s.contains(':') {
// try to parse as socket address
let addr = s
.parse::<SocketAddr>()
.map_err(|_| io_error("invalid socket address"))?;

Ok(AddressType::SocketAddr(addr))
} else {
// try to parse as path
let path = PathBuf::from(s);
Ok(AddressType::PathBuf(path))
}
}
}

impl ToSocketAddrs for AddressType {
type Iter = std::vec::IntoIter<SocketAddr>;

fn to_socket_addrs(&self) -> io::Result<Self::Iter> {
match self {
AddressType::SocketAddr(addr) => Ok(vec![*addr].into_iter()),
AddressType::PathBuf(_) => Err(io_error("path is not a valid socket address")),
}
}
}

impl From<AddressType> for PathBuf {
fn from(val: AddressType) -> Self {
match val {
AddressType::SocketAddr(_) => panic!("socket address is not a valid path"),
AddressType::PathBuf(path) => path,
}
}
}

/// A transport provides connection-oriented communication between two peers through
/// ordered and reliable streams of bytes.
///
Expand Down

0 comments on commit f0cd1e3

Please sign in to comment.