Struct wstp::Link

source · []
#[repr(transparent)]
pub struct Link { /* private fields */ }
Expand description

WSTP link endpoint.

WSClose() is called on the underlying WSLINK when Drop::drop() is called for a value of this type.

WSTP C API Documentation: WSLINK

Wolfram Language Documentation: LinkObject

Implementations

WSTP C API Documentation: WSWaitForLinkActivity

Wait for data to become available, periodically calling a callback.

true will be returned if data is available. false will be returned if the callback returns Break.

Example
use wstp::{Link, Protocol};

let mut listener = Link::listen(Protocol::IntraProcess, "").unwrap();

let mut counter = 0;

listener
    .wait_with_callback(|_: &mut Link| {
        use std::ops::ControlFlow;

        counter += 1;

        if counter < 5 {
            ControlFlow::Continue(())
        } else {
            ControlFlow::Break(())
        }
    })
    .unwrap();
User data fields

This function will temporarily replace any user data values (set using Link::set_user_data) which are associated with the current link. The user data values on the &mut Link parameter inside the callback are an implementation detail of this function and must not be modified.

WSTP C API Documentation: WSWaitForLinkActivityWithCallback

Get the type of the next token available to read on this link.

See also Link::get_token().

Read the next token from this link.

See also Link::get_type().

Example

Read the expression {5, "second", foo} from a link one Token at a time:

use wstp::{Link, Token};

// Put {5, "second", foo}
let mut link = Link::new_loopback().unwrap();
link.put_function("System`List", 3).unwrap();
link.put_i64(5).unwrap();
link.put_str("second").unwrap();
link.put_symbol("Global`foo").unwrap();

// Read it back
assert!(matches!(link.get_token().unwrap(), Token::Function { length: 3 }));
assert!(matches!(link.get_token().unwrap(), Token::Symbol(s) if s.as_str() == "System`List"));
assert!(matches!(link.get_token().unwrap(), Token::Integer(5)));
assert!(matches!(link.get_token().unwrap(), Token::String(s) if s.as_str() == "second"));
assert!(matches!(link.get_token().unwrap(), Token::Symbol(s) if s.as_str() == "Global`foo"));

Get the raw type of the next token available to read on this link.

If the returned type is WSTKERR, an error is returned.

See also Link::get_type().

WSTP C API Documentation: WSGetType()

WSTP C API Documentation: WSGetUTF8String()

Convenience wrapper around Link::get_string_ref().

WSTP C API Documentation: WSGetUTF8Symbol()

WSTP C API Documentation: WSGetUTF8String()

WSTP C API Documentation: WSGetUTF16String()

WSTP C API Documentation: WSGetUTF32String()

Check that the incoming expression is a function with head symbol.

If the check succeeds, the number of elements in the incoming expression is returned. Otherwise, an error is returned.

Example
use wstp::Link;

#[derive(Debug, PartialEq)]
struct Quantity {
    value: f64,
    unit: String,
}

fn get_quantity(link: &mut Link) -> Result<Quantity, wstp::Error> {
    // Use test_head() to verify that the incoming expression has the expected
    // head.
    let argc = link.test_head("System`Quantity")?;

    assert!(argc == 2, "expected Quantity to have 2 arguments");

    let value = link.get_f64()?;
    let unit = link.get_string()?;

    Ok(Quantity { value, unit })
}

let mut link = Link::new_loopback().unwrap();
link.put_function("System`Quantity", 2).unwrap();
link.put_f64(5.0).unwrap();
link.put_str("Seconds").unwrap();

assert_eq!(
    get_quantity(&mut link),
    Ok(Quantity { value: 5.0, unit: "Seconds".into() })
);

Check that the incoming expression is a function with head symbol.

This method is an optimized variant of Link::test_head().

WSTP C API Documentation: WSGetArgCount()

WSTP C API Documentation: WSGetInteger64()

WSTP C API Documentation: WSGetInteger32()

WSTP C API Documentation: WSGetInteger16()

WSTP C API Documentation: WSGetInteger8()

WSTP C API Documentation: WSGetReal64()

WSTP C API Documentation: WSGetReal32()

Get a multidimensional array of i64.

Example
use wstp::Link;

let mut link = Link::new_loopback().unwrap();

link.put_i64_array(&[1, 2, 3, 4], &[2, 2]).unwrap();

let out = link.get_i64_array().unwrap();

assert_eq!(out.data().len(), 4);
assert_eq!(out.dimensions(), &[2, 2]);

WSTP C API Documentation: WSGetInteger64Array()

WSTP C API Documentation: WSGetInteger32Array()

WSTP C API Documentation: WSGetInteger16Array()

WSTP C API Documentation: WSGetInteger8Array()

Get a multidimensional array of f64.

Example
use wstp::Link;

let mut link = Link::new_loopback().unwrap();

link.put_f64_array(&[3.141, 1.618, 2.718], &[3]).unwrap();

let out = link.get_f64_array().unwrap();

assert_eq!(out.data().len(), 3);
assert_eq!(out.data(), &[3.141, 1.618, 2.718]);
assert_eq!(out.dimensions(), &[3]);

WSTP C API Documentation: WSGetReal64Array()

WSTP C API Documentation: WSGetReal32Array()

TODO: Augment this function with a put_type() method which takes a (non-exhaustive) enum value.

WSTP C API Documentation: WSPutType()

WSTP C API Documentation: WSEndPacket()

WSTP C API Documentation: WSPutUTF8String()

WSTP C API Documentation: WSPutUTF8Symbol()

WSTP C API Documentation: WSPutUTF8String()

This function will return a WSTP error if utf8 is not a valid UTF-8 encoded string.

Put a UTF-16 encoded string.

This function will return a WSTP error if utf16 is not a valid UTF-16 encoded string.

WSTP C API Documentation: WSPutUTF16String()

Put a UTF-32 encoded string.

This function will return a WSTP error if utf32 is not a valid UTF-32 encoded string.

WSTP C API Documentation: WSPutUTF32String()

Begin putting a function onto this link.

Examples

Put the expression {1, 2, 3} on the link:

let mut link = Link::new_loopback()?;

link.put_function("System`List", 3)?;
link.put_i64(1)?;
link.put_i64(2)?;
link.put_i64(3)?;

Put the expression foo["a"]["b"] on the link:

let mut link = Link::new_loopback()?;

link.put_function(None, 1)?;
link.put_function("Global`foo", 1)?;
link.put_str("a")?;
link.put_str("b")?;

WSTP C API Documentation: WSPutArgCount()

WSTP C API Documentation: WSPutInteger64()

WSTP C API Documentation: WSPutInteger32()

WSTP C API Documentation: WSPutInteger16()

WSTP C API Documentation: WSPutInteger8()

WSTP C API Documentation: WSPutReal64()

WSTP C API Documentation: WSPutReal32()

Put a multidimensional array of i64.

Panics

This function will panic if the product of dimensions is not equal to data.len().

WSTP C API Documentation: WSPutInteger64Array()

Put a multidimensional array of i32.

Panics

This function will panic if the product of dimensions is not equal to data.len().

WSTP C API Documentation: WSPutInteger32Array()

Put a multidimensional array of i16.

Panics

This function will panic if the product of dimensions is not equal to data.len().

WSTP C API Documentation: WSPutInteger16Array()

WSTP C API Documentation: WSPutInteger8Array()

Put a multidimensional array of f64.

Panics

This function will panic if the product of dimensions is not equal to data.len().

WSTP C API Documentation: WSPutReal64Array()

Put a multidimensional array of f32.

Panics

This function will panic if the product of dimensions is not equal to data.len().

WSTP C API Documentation: WSPutReal32Array()

Put an EvaluatePacket[expr] onto the link.

Transmute a &mut WSLINK into a &mut Link.

This operation enables usage of the safe Link wrapper type without assuming ownership over the underying raw WSLINK.

Use this function to construct a Link from a borrowed WSLINK. This function should be used in LibraryLink functions loaded via LibraryFunctionLoad instead of Link::unchecked_new().

Safety

For this operation to be safe, the caller must ensure:

  • the WSLINK is validly initialized.
  • they have unique ownership of the WSLINK value; no aliasing is possible.

and the maintainer of this functionality must ensure:

  • The Link type is a #[repr(transparent)] wrapper around around a single field of type WSLINK.

Create a new Loopback type link.

WSTP C API Documentation: WSLoopbackOpen()

Create a new named WSTP link using protocol.

Connect to an existing named WSTP link.

Create a new WSTP TCPIP link bound to addr.

If addr yields multiple addresses, listening will be attempted with each of the addresses until one succeeds and returns the listener. If none of the addresses succeed in creating a listener, the error returned from the last attempt (the last address) is returned.

Connect to an existing WSTP TCPIP link listening at addr.

If addr yields multiple addresses, a connection will be attempted with each of the addresses until a connection is successful. If none of the addresses result in a successful connection, the error returned from the last connection attempt (the last address) is returned.

Open a WSTP Protocol::TCPIP connection to a LinkServer.

If addrs yields multiple addresses, a connection will be attempted with each of the addresses until a connection is successful. If none of the addresses result in a successful connection, the error returned from the last connection attempt (the last address) is returned.

WSTP C API Documentation: WSOpenArgcArgv()

This function can be used to create a Link of any protocol and mode. Prefer to use one of the constructor methods listed below when you know the type of link to be created.

Construct a Link from a raw WSLINK pointer.

WSTP C API Documentation: WSActivate()

Close this end of the link.

WSTP C API Documentation: WSClose()

Get the name of this link.

WSTP C API Documentation: WSLinkName()

Check if there is data ready to be read from this link.

WSTP C API Documentation: WSReady()

WSTP C API Documentation: WSIsLinkLoopback()

Returns an Error describing the last error to occur on this link.

Examples

TODO: Example of getting an error code.

Returns a string describing the last error to occur on this link.

TODO: If the most recent operation was successful, does the error message get cleared?

WSTP C API Documentation: WSErrorMessage()

Clear errors on this link.

WSTP C API Documentation: WSClearError()

WSTP C API Documentation: WSLINK

WSTP C API Documentation: WSUserData

WSTP C API Documentation: WSSetUserData

Flush out any buffers containing data waiting to be sent on this link.

WSTP C API Documentation: WSFlush()

WSTP C API Documentation: WSGetNext()

WSTP C API Documentation: WSNextPacket()

WSTP C API Documentation: WSNewPacket()

Read an expression off of this link.

Write an expression to this link.

Transfer an expression from this link to another.

Example

Transfer an expression between two loopback links:

use wstp::Link;

let mut a = Link::new_loopback().unwrap();
let mut b = Link::new_loopback().unwrap();

// Put an expression into `a`
a.put_i64(5).unwrap();

// Transfer it to `b`
a.transfer_expr_to(&mut b).unwrap();

assert_eq!(b.get_i64().unwrap(), 5);

WSTP C API Documentation: WSTransferExpression()

Transfer the full contents of this loopback link to dest.

WSTP C API Documentation: WSTransferToEndOfLoopbackLink()

Panics

This function will panic if !self.is_loopback().

Trait Implementations

Formats the value using the given formatter. Read more
Executes the destructor for this type. Read more

Safety

Links can be sent between threads, but they cannot be used from multiple threads at once (unless WSEnableLinkLock() has been called on the link). So Link satisfies Send but not Sync.

TODO: Add a wrapper type for Link which enforces that WSEnableLinkLock() has been called, and implements Sync.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.