forked from rust-in-action/code
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
131 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
use std::fs::File; | ||
|
||
fn main() -> Result<(), std::io::Error> { | ||
let _f = File::open("invisible.txt")?; | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
use std::fmt; | ||
use std::fmt::Display; | ||
|
||
#[derive(Debug)] | ||
struct MacAddress([u8; 6]); | ||
|
||
impl Display for MacAddress { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
let octet = self.0; | ||
write!( | ||
f, | ||
"{:02x}:{:02x}:{:02x}:{:02x}:{:02x}:{:02x}", | ||
octet[0], octet[1], octet[2], octet[3], octet[4], octet[5] | ||
) | ||
} | ||
} | ||
|
||
impl MacAddress { | ||
fn is_local(&self) -> bool { | ||
(self.0[0] & 0b_0000_0010) == 0 | ||
} | ||
|
||
fn is_universal(&self) -> bool { | ||
!self.is_local() | ||
} | ||
|
||
fn is_unicast(&self) -> bool { | ||
(self.0[0] & 0b_0000_0001) == 0 | ||
} | ||
|
||
fn is_multicast(&self) -> bool { | ||
!self.is_unicast() | ||
} | ||
} | ||
|
||
fn main() -> Result<(), std::io::Error> { | ||
let mac = MacAddress([0x6, 0, 0, 0, 0, 0]); | ||
println!("mac: {}", mac); | ||
println!("mac: {:?}", mac); | ||
println!("universal?: {:?}", mac.is_universal()); | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
use std::fs::File; | ||
use std::net::Ipv6Addr; | ||
|
||
fn main() -> Result<(), std::io::Error> { | ||
let _f = File::open("invisible.txt")?; | ||
|
||
let _localhost = "::1" | ||
.parse::<Ipv6Addr>()?; | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
use std::fs::File; | ||
use std::error::Error; | ||
use std::net::Ipv6Addr; | ||
|
||
fn main() -> Result<(), Box<dyn Error>> { | ||
let _f = File::open("invisible.txt")?; | ||
let _localhost = "::1".parse::<Ipv6Addr>()?; | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
use std::io; | ||
use std::fmt; | ||
use std::net; | ||
use std::fs::File; | ||
use std::net::Ipv6Addr; | ||
|
||
#[derive(Debug)] | ||
enum UpstreamError{ | ||
IO(io::Error), | ||
Parsing(net::AddrParseError), | ||
} | ||
|
||
impl fmt::Display for UpstreamError { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
write!(f, "{:?}", self) // <1> Implement Display in terms of Debug | ||
} | ||
} | ||
|
||
fn main() -> Result<(), UpstreamError> { | ||
let _f = File::open("invisible.txt").map_err(UpstreamError::IO)?; | ||
let _localhost = "::1".parse::<Ipv6Addr>().map_err(UpstreamError::Parsing)?; | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
use std::io; | ||
use std::fmt; | ||
use std::net; | ||
use std::fs::File; | ||
use std::net::Ipv6Addr; | ||
|
||
#[derive(Debug)] | ||
enum UpstreamError{ | ||
IO(io::Error), | ||
Parsing(net::AddrParseError), | ||
} | ||
|
||
impl fmt::Display for UpstreamError { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
write!(f, "{:?}", self) // <1> Implement Display in terms of Debug | ||
} | ||
} | ||
|
||
impl From<io::Error> for UpstreamError { | ||
fn from(error: io::Error) -> Self { | ||
UpstreamError::IO(error) | ||
} | ||
} | ||
|
||
impl From<net::AddrParseError> for UpstreamError { | ||
fn from(error: net::AddrParseError) -> Self { | ||
UpstreamError::Parsing(error) | ||
} | ||
} | ||
|
||
fn main() -> Result<(), UpstreamError> { | ||
let _f = File::open("invisible.txt").map_err(UpstreamError::IO)?; | ||
let _localhost = "::1".parse::<Ipv6Addr>().map_err(UpstreamError::Parsing)?; | ||
|
||
Ok(()) | ||
} |