ipzone/
port.rs

1use super::*;
2use std::{num::ParseIntError, str::FromStr};
3
4#[inline]
5pub fn from(source: impl Into<u16>) -> Port {
6    Port(source.into())
7}
8#[inline]
9pub fn from_str(s: &str) -> Result<u16, ParseIntError> {
10    _from_str(s)
11}
12#[inline]
13pub fn from_env(key: &str) -> Result<u16, FromEnvError> {
14    _from_env(key)
15}
16
17#[repr(transparent)]
18#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
19pub struct Port(pub u16);
20
21impl From<Port> for u16 {
22    #[inline]
23    fn from(port: Port) -> Self {
24        port.0
25    }
26}
27
28impl From<u16> for Port {
29    #[inline]
30    fn from(port: u16) -> Self {
31        Self(port)
32    }
33}
34impl TryFrom<&str> for Port {
35    type Error = ParseIntError;
36
37    #[inline]
38    fn try_from(s: &str) -> Result<Self, Self::Error> {
39        Ok(Self(_from_str(s)?))
40    }
41}
42impl FromStr for Port {
43    type Err = ParseIntError;
44    #[inline]
45    fn from_str(s: &str) -> Result<Self, Self::Err> {
46        Ok(Self(s.parse()?))
47    }
48}