pub enum Address<'a> {
Unknown,
IPv4(Ipv4Addr, NonZeroU16),
ComPort(NonZeroU8),
TtyDevice(TextID<'a>),
}
Expand description
A data structure holding a controller’s physical address.
Variants§
Unknown
Address unknown.
IPv4(Ipv4Addr, NonZeroU16)
An IP v.4 address plus port.
ComPort(NonZeroU8)
A Windows COM port.
TtyDevice(TextID<'a>)
A UNIX-style tty serial port device.
Implementations§
Source§impl<'a> Address<'a>
impl<'a> Address<'a>
Sourcepub fn new_ipv4(addr: &str, port: u16) -> Result<Self, String>
pub fn new_ipv4(addr: &str, port: u16) -> Result<Self, String>
Create a new Address::IPv4
from an IP address string and port number.
The IP address cannot be unspecified (e.g. 0.0.0.0
).
The IP port cannot be zero.
§Errors
Returns Err(String)
if:
- The IP address string is invalid,
- The IP address is unspecified (e.g.
0.0.0.0
), - The IP port is zero.
§Error Examples
assert_eq!(Err("invalid IP address: [hello]".into()), Address::new_ipv4("hello", 123));
assert_eq!(Err("IP port cannot be zero".into()), Address::new_ipv4("1.02.003.004", 0));
assert_eq!(Err("invalid null IP address".into()), Address::new_ipv4("0.00.000.0", 123));
§Examples
assert_eq!(
Address::IPv4(Ipv4Addr::from_str("1.2.3.4").unwrap(), NonZeroU16::new(5).unwrap()),
Address::new_ipv4("1.02.003.004", 5)?
);
Sourcepub fn new_com_port(port: u8) -> Result<Self, String>
pub fn new_com_port(port: u8) -> Result<Self, String>
Create a new Address::ComPort
from a Windows serial port number.
The COM port number cannot be zero.
§Errors
Returns Err(String)
if the COM port number is zero.
§Error Examples
assert_eq!(Err("COM port cannot be zero".into()), Address::new_com_port(0));
§Examples
assert_eq!(
Address::ComPort(NonZeroU8::new(5).unwrap()),
Address::new_com_port(5)?
);
Sourcepub fn new_tty_device(device: &'a str) -> Result<Self, String>
pub fn new_tty_device(device: &'a str) -> Result<Self, String>
Create a new Address::TtyDevice
from a UNIX-style tty device name.
The device name should start with tty
.
§Errors
Returns Err(String)
if the device name is not valid for a tty.
§Error Examples
assert_eq!(Err("invalid tty device: [hello]".into()), Address::new_tty_device("hello"));
§Examples
assert_eq!(
Address::TtyDevice(TextID::new("ttyHello").unwrap()),
Address::new_tty_device("ttyHello")?
);
Trait Implementations§
Source§impl<'a, 'de: 'a> Deserialize<'de> for Address<'a>
impl<'a, 'de: 'a> Deserialize<'de> for Address<'a>
Source§fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>
Source§impl<'a> TryFrom<&'a str> for Address<'a>
impl<'a> TryFrom<&'a str> for Address<'a>
Source§fn try_from(item: &'a str) -> Result<Self, Self::Error>
fn try_from(item: &'a str) -> Result<Self, Self::Error>
Parse a text string into an Address
.
§Errors
Returns Err(String)
if the input string is not recognized as a valid address.
§Error Examples
// The following should error because port cannot be zero if IP address is not zero
assert_eq!(
Err("IP port cannot be zero".into()),
Address::try_from("1.02.003.004:0")
);
// The following should error because port must be zero if IP address is zero
assert_eq!(
Err("null IP must have zero port number".into()),
Address::try_from("0.0.0.0:123")
);
§Examples
assert_eq!(
Address::IPv4(Ipv4Addr::from_str("1.2.3.4").unwrap(), NonZeroU16::new(5).unwrap()),
Address::try_from("1.02.003.004:05")?
);
// 0.0.0.0:0 is OK because both IP address and port are zero
assert_eq!(Address::Unknown, Address::try_from("0.0.0.0:0")?);
assert_eq!(
Address::ComPort(NonZeroU8::new(123).unwrap()),
Address::try_from("COM123")?
);
assert_eq!(
Address::TtyDevice(TextID::new("ttyABC").unwrap()),
Address::try_from("ttyABC")?
);
impl<'a> Eq for Address<'a>
impl<'a> StructuralPartialEq for Address<'a>
Auto Trait Implementations§
impl<'a> Freeze for Address<'a>
impl<'a> RefUnwindSafe for Address<'a>
impl<'a> Send for Address<'a>
impl<'a> Sync for Address<'a>
impl<'a> Unpin for Address<'a>
impl<'a> UnwindSafe for Address<'a>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.