Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,24 @@ impl Default for SntpRequest {

impl SntpRequest {
/// Creates a new SNTP request object.
///
/// # Panics
///
/// Panics if the UDP socket cannot be bound or the timeout cannot be set.
/// Use [`try_new`](Self::try_new) for a non-panicking alternative.
pub fn new() -> SntpRequest {
Self::try_new().expect("failed to create SNTP request")
}

/// Creates a new SNTP request object, returning an error instead of
/// panicking if the UDP socket cannot be bound or the timeout cannot be set.
pub fn try_new() -> io::Result<SntpRequest> {
let sntp = SntpRequest {
socket: UdpSocket::bind("0.0.0.0:0").unwrap(),
socket: UdpSocket::bind("0.0.0.0:0")?,
kiss_of_death: Cell::new(false),
};
sntp.set_timeout(Duration::from_secs(5)).unwrap();
sntp
sntp.set_timeout(Duration::from_secs(5))?;
Ok(sntp)
}

#[inline]
Expand Down