Struct Port

Source
pub struct Port { /* private fields */ }
Expand description

The port object represents a Haiku port

There are two types of ports: there are owned ports, which means that they are created with this API. An owned port will live as long as the Port object lives. Owned ports are created with the Ports::create() method. There are also borrowed ports. These are retrieved using Ports::find_port(). These ports will outlive the lifetime of the Port object.

In terms of usage safety, ports are very badly designed on Haiku. While a port does have an owning team, this merely means the port is deleted when the team is. It does not give any additional privileges. This means that anyone can read from every port, and even delete every port.

This API makes the assumption that there is one owner of a port. This owner can read from the port, and has control over closing it. Other actors should not (and cannot). This means that reading from a port is only possible if you own it. If you try to read from a port that you do not own, the library will panic. You do not need to be the owner to write to a port.

Implementations§

Source§

impl Port

Source

pub fn create(name: &str, capacity: i32) -> Result<Port>

Create a new port and take ownership of it

This method creates a new port and takes ownership of that port. The name parameter should be no more than 32 characters. The capacity should be zero or higher. On success you will get a new port object.

Source

pub fn find(name: &str) -> Option<Port>

Find an existing port by name

If the port exists, this function will return a borrowed Port object. This means that the port will not be deleted when the object goes out of scope.

Source

pub fn from_id(id: port_id) -> Option<Port>

Construct a borrowed port from id

If the port exists, this function will return a borrowed Port object. This means that the port will not be deleted when the object goes out of scope.

Source

pub fn write(&self, type_code: i32, data: &[u8]) -> Result<()>

Write data to the port

The data is identified by a type_code and is sent as an array of bytes. If the port has already reached its maximum capacity, this operation will block until the message can be written.

Source

pub fn try_write( &self, type_code: i32, data: &[u8], timeout: Duration, ) -> Result<()>

Attempt to write data to the port

The data is identified by a type_code and is sent as an array of bytes. If the port has already reached its maximum capacity, this operation will block until the message can be written, or until the timeout is reached. Set the timeout to 0 if you want to return immediately if the port is at capacity.

Source

pub fn read(&self) -> Result<(i32, Vec<u8>)>

Read data from a port

This method reads the next message from the port. The data is returned as a tuple of a type code and a buffer. The method waits until there is a next message.

Source

pub fn try_read(&self, timeout: Duration) -> Result<(i32, Vec<u8>)>

Attempt to read data from a port

This method reads the next message from the port. The data is returned as a tuple of a type code and a buffer. The method waits until there is a next message, or until when a timeout if reached. If you don’t want to wait for a message to come in, you can set the timeout to 0

Source

pub fn close(&self) -> Result<()>

Close a port

When a port is closed, data can no longer be written to it. The message queue can still be read. Once a port is closed, it cannot be reopened.

Source

pub fn get_count(&self) -> Result<usize>

Get the port count

This returns the number of items that are waiting to be processed.

Source

pub fn get_info(&self) -> Result<PortInfo>

Get the port info

Source

pub fn get_port_id(&self) -> port_id

Get the underlying port id

Trait Implementations§

Source§

impl Clone for Port

Source§

fn clone(&self) -> Self

Create a borrowed clone of the Port

A clone is always borrowed, whether the original is owned or not. The implication is that you have to make sure that the original Port will outlive any of its clones, if you want to keep using any clones.

1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Drop for Port

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl Freeze for Port

§

impl RefUnwindSafe for Port

§

impl Send for Port

§

impl Sync for Port

§

impl Unpin for Port

§

impl UnwindSafe for Port

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.