Skip to main content

ConnectOptions

Struct ConnectOptions 

Source
#[non_exhaustive]
pub struct ConnectOptions { pub replica_id: String, pub from: FeedPosition, pub timeout: Duration, pub security: Option<ReplicaSecurity>, }
Expand description

Everything a replica presents when it connects, beyond the address. The defaults are a fresh replica: generation 0 (no continuity claim), offset 0, a 5 s connect timeout, a plaintext link.

use std::time::Duration;
use kevy_replicate::feed::FeedPosition;
use kevy_replicate::replica::ConnectOptions;

let opts = ConnectOptions::new("replica-a")
    .with_from(FeedPosition::new(7, 42))
    .with_timeout(Duration::from_secs(1));
assert_eq!(opts.from, FeedPosition::new(7, 42));

Fields (Non-exhaustive)§

This struct is marked as non-exhaustive
Non-exhaustive structs could have additional fields added in future. Therefore, non-exhaustive structs cannot be constructed in external crates using the traditional Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.
§replica_id: String

The replica’s identifier, operator-set; the primary keys its slot by it.

use kevy_replicate::handshake::HandshakeReq;
use kevy_replicate::replica::{ConnectOptions, ReplicaClient};

drop(ReplicaClient::connect_with(addr, &ConnectOptions::new("replica-a"))?);
// the primary sees the id in the handshake
let sent = primary.join().expect("primary thread");
let mut argv = kevy_resp::Argv::default();
kevy_resp::parse_command_into(&sent, &mut argv)?;
assert_eq!(HandshakeReq::parse(&argv)?.replica_id, "replica-a");
§from: FeedPosition

Where to resume: the feed generation this replica’s data reflects (0 = unknown or fresh) and the offset within it. The primary serves the offset’s continuity only when the generations match; otherwise it ships a snapshot.

use kevy_replicate::feed::FeedPosition;
use kevy_replicate::replica::{ConnectOptions, ReplicaClient};

let opts = ConnectOptions::new("replica-a").with_from(FeedPosition::new(7, 42));
let client = ReplicaClient::connect_with(addr, &opts)?;
assert_eq!(client.expected_offset(), opts.from.offset); // resumes where its data ends
§timeout: Duration

How long the connect and the handshake may take.

use std::time::Duration;
use kevy_replicate::replica::{ConnectOptions, ReplicaClient, ReplicaError};

// a listener that never answers the handshake
let silent = std::net::TcpListener::bind("127.0.0.1:0")?;
let opts = ConnectOptions::new("replica-a").with_timeout(Duration::from_millis(50));
let err = ReplicaClient::connect_with(silent.local_addr()?, &opts).unwrap_err();
assert!(matches!(err, ReplicaError::Io(_)));
§security: Option<ReplicaSecurity>

Some to open a Noise IK link instead of a plaintext one.

use kevy_replicate::replica::{ConnectOptions, ReplicaSecurity};

assert!(ConnectOptions::new("r1").security.is_none()); // plaintext by default
let sec = ReplicaSecurity::new(kevy_noise::Keypair::from_secret([2; 32]), [9; 32]);
let opts = ConnectOptions::new("r1").with_security(sec);
assert_eq!(opts.security.map(|s| s.primary_key), Some([9; 32]));

Implementations§

Source§

impl ConnectOptions

Source

pub fn new(replica_id: impl Into<String>) -> Self

A fresh replica named replica_id, with the defaults above.

let opts = kevy_replicate::replica::ConnectOptions::new("r1");
assert_eq!(opts.from, kevy_replicate::feed::FeedPosition::default());
Source

pub fn with_from(self, from: FeedPosition) -> Self

Set ConnectOptions::from.

use kevy_replicate::feed::FeedPosition;

let opts = kevy_replicate::replica::ConnectOptions::new("r1").with_from(FeedPosition::new(3, 9));
assert_eq!(opts.from, FeedPosition::new(3, 9));
Source

pub fn with_timeout(self, timeout: Duration) -> Self

Set ConnectOptions::timeout.

use std::time::Duration;
let opts = kevy_replicate::replica::ConnectOptions::new("r1").with_timeout(Duration::from_secs(1));
assert_eq!(opts.timeout, Duration::from_secs(1));
Source

pub fn with_security(self, security: ReplicaSecurity) -> Self

Open the link under Noise IK with security.

use kevy_replicate::replica::{ConnectOptions, ReplicaSecurity};

let sec = ReplicaSecurity::new(kevy_noise::Keypair::from_secret([2; 32]), [9; 32]);
let opts = ConnectOptions::new("r1").with_security(sec);
assert!(opts.security.is_some());

Trait Implementations§

Source§

impl Clone for ConnectOptions

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for ConnectOptions

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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 = !

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

fn try_from(value: U) -> Result<T, !>

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.