Trait Ops

Source
pub trait Ops {
    // Required methods
    fn any(host: &str) -> Option<u16>;
    fn in_range(host: &str, r: Range) -> Option<u16>;
    fn from_list(host: &str, v: Vec<u16>) -> Option<u16>;
    fn except(host: &str, v: Vec<u16>) -> Option<u16>;
    fn in_range_except(host: &str, r: Range, v: Vec<u16>) -> Option<u16>;
    fn is_port_available(host: &str, p: u16) -> bool;
}
Expand description

A trait for defining behaviour on the lib’s functions

Required Methods§

Source

fn any(host: &str) -> Option<u16>

Returns any port in default range.

§Arguments
  • host - a string slice pointing to the hostname to test the port availability on.
§Examples
use get_port::tcp::TcpPort;
use get_port::udp::UdpPort;
use get_port::Ops;

let tcp_port = TcpPort::any("127.0.0.1").unwrap();
let udp_port = UdpPort::any("127.0.0.1").unwrap();
Source

fn in_range(host: &str, r: Range) -> Option<u16>

Returns any port in given range.

§Arguments
  • host - a string slice pointing to the hostname to test the port availability on.
  • r - the Range to choose a port from.
§Examples
use get_port::tcp::TcpPort;
use get_port::{Ops, Range};
use get_port::udp::UdpPort;

let tcp_port = TcpPort::in_range("127.0.0.1", Range {min: 6000, max: 7000 }).unwrap();
let udp_port = UdpPort::in_range("127.0.0.1", Range {min: 8000, max: 9000 }).unwrap();
Source

fn from_list(host: &str, v: Vec<u16>) -> Option<u16>

Returns any port from the supplied list.

§Arguments
  • host - a string slice pointing to the hostname to test the port availability on.
  • v - a vector of Ports (u16) to choose from.
§Examples
use get_port::tcp::TcpPort;
use get_port::Ops;
use get_port::udp::UdpPort;

let tcp_port = TcpPort::from_list("127.0.0.1", vec![5000, 6000]).unwrap();
let udp_port = UdpPort::from_list("127.0.0.1", vec![5000, 6000]).unwrap();
Source

fn except(host: &str, v: Vec<u16>) -> Option<u16>

Returns any port from the default range except for the ones in the supplied list.

§Arguments
  • host - a string slice pointing to the hostname to test the port availability on.
  • v - a vector of Ports (u16) to exclude.
§Examples
use get_port::tcp::TcpPort;
use get_port::Ops;
use get_port::udp::UdpPort;

let tcp_port = TcpPort::except("127.0.0.1", vec![1456, 6541]).unwrap();
let udp_port = UdpPort::except("127.0.0.1", vec![1456, 6541]).unwrap();
Source

fn in_range_except(host: &str, r: Range, v: Vec<u16>) -> Option<u16>

Returns any port from the supplied range except for the ones in the supplied list.

§Arguments
  • host - a string slice pointing to the hostname to test the port availability on.
  • r - the Range to choose a port from.
  • v - a vector of Ports (u16) to exclude.
§Examples
use get_port::tcp::TcpPort;
use get_port::{Ops, Range};
use get_port::udp::UdpPort;

let tcp_port = TcpPort::in_range_except("127.0.0.1", Range { min: 6000, max: 7000 }, vec![6500]).unwrap();
let udp_port = UdpPort::in_range_except("127.0.0.1", Range { min: 6000, max: 7000 }, vec![6500]).unwrap();
Source

fn is_port_available(host: &str, p: u16) -> bool

Utility function to check whether a port is available or not.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§