pub struct PortPicker { /* private fields */ }
Expand description
PortPicker is a simple library to pick a free port in the local machine.
It can be used to find a free port to start a server or any other use case.
#Examples:
use random_port::PortPicker;
let port = PortPicker::new().pick().unwrap();
println!("The free port is {}", port);
Implementations§
Source§impl PortPicker
impl PortPicker
pub fn new() -> Self
Sourcepub fn port_range(self, range: RangeInclusive<u16>) -> Self
pub fn port_range(self, range: RangeInclusive<u16>) -> Self
Specifies the range of ports to check. Must be in the range 1024..=65535
. E.g. port_range(1024..=65535)
.
Sourcepub fn execlude_add(self, port: u16) -> Self
pub fn execlude_add(self, port: u16) -> Self
Specifies a port to exclude.
Sourcepub fn protocol(self, protocol: Protocol) -> Self
pub fn protocol(self, protocol: Protocol) -> Self
Specifies the protocol to check, Default is Protocol::All
. Can be either Protocol::Tcp
, Protocol::Udp
or Protocol::All
.
Sourcepub fn host(self, host: String) -> Self
pub fn host(self, host: String) -> Self
Specifies the host to check. Can be either an Ipv4 or Ipv6 address. If not specified, will checks availability on all local addresses defined in the system.
Sourcepub fn random(self, random: bool) -> Self
pub fn random(self, random: bool) -> Self
Specifies whether to pick a random port from the range. If not specified, will pick the first available port from the range.
pub fn pick(&self) -> Result<u16>
Auto Trait Implementations§
impl Freeze for PortPicker
impl RefUnwindSafe for PortPicker
impl Send for PortPicker
impl Sync for PortPicker
impl Unpin for PortPicker
impl UnwindSafe for PortPicker
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
Mutably borrows from an owned value. Read more