pub struct NetTransportBuilder<P, C = JsonCodec>where
P: Providers,
C: MessageCodec,{ /* private fields */ }Expand description
Builder for NetTransport that eliminates common footguns.
The manual Arc wrapping and set_weak_self() pattern is error-prone:
forgetting set_weak_self() causes a runtime panic. This builder handles
both automatically.
§Examples
// Standard usage - server or client that needs RPC responses:
let transport = NetTransportBuilder::new(providers)
.local_address(addr)
.build_listening()
.await?;
// Fire-and-forget sender (no listening needed):
let transport = NetTransportBuilder::new(providers)
.local_address(addr)
.build();
// With custom peer config:
let transport = NetTransportBuilder::new(providers)
.local_address(addr)
.peer_config(config)
.build_listening()
.await?;§Why Build vs Build Listening?
-
build_listening(): For most RPC use cases. Both servers AND clients need this because responses are sent to the client’s listening address. -
build(): For fire-and-forget messaging where you don’t expect responses. Also useful for testing where you control message flow manually.
Implementations§
Source§impl<P> NetTransportBuilder<P>where
P: Providers,
impl<P> NetTransportBuilder<P>where
P: Providers,
Source§impl NetTransportBuilder<TokioProviders>
impl NetTransportBuilder<TokioProviders>
Sourcepub fn tokio() -> NetTransportBuilder<TokioProviders>
pub fn tokio() -> NetTransportBuilder<TokioProviders>
Create a builder backed by the production
TokioProviders bundle.
Shorthand for NetTransportBuilder::new(TokioProviders::new()) — the
usual production entry point.
Source§impl<P, C> NetTransportBuilder<P, C>
impl<P, C> NetTransportBuilder<P, C>
Sourcepub fn codec<NewC>(self, codec: NewC) -> NetTransportBuilder<P, NewC>where
NewC: MessageCodec,
pub fn codec<NewC>(self, codec: NewC) -> NetTransportBuilder<P, NewC>where
NewC: MessageCodec,
Set the message codec for this transport.
The codec is used for all serialization/deserialization in RPC handlers and client endpoints created from this transport.
Sourcepub fn local_address(self, address: NetworkAddress) -> NetTransportBuilder<P, C>
pub fn local_address(self, address: NetworkAddress) -> NetTransportBuilder<P, C>
Set the local address for this transport.
This is required before calling build() or build_listening().
§Arguments
address- The network address to bind to
Sourcepub fn peer_config(self, config: PeerConfig) -> NetTransportBuilder<P, C>
pub fn peer_config(self, config: PeerConfig) -> NetTransportBuilder<P, C>
Set custom peer configuration.
If not set, uses PeerConfig::default().
Sourcepub fn build(self) -> Result<Arc<NetTransport<P, C>>, MessagingError>
pub fn build(self) -> Result<Arc<NetTransport<P, C>>, MessagingError>
Build the transport without starting the listener.
Returns Arc<NetTransport> with set_weak_self() already called.
Use this for fire-and-forget messaging or testing.
For RPC (request/response), use build_listening() instead.
§Errors
Returns MessagingError::MissingLocalAddress if local_address() was not called.
Sourcepub async fn build_listening(
self,
) -> Result<Arc<NetTransport<P, C>>, MessagingError>
pub async fn build_listening( self, ) -> Result<Arc<NetTransport<P, C>>, MessagingError>
Build the transport and start listening for incoming connections.
Returns Arc<NetTransport> with set_weak_self() already called
and the listener started.
Use this for typical RPC usage where you need to receive responses. Both servers AND clients need this in a request/response pattern.
§Errors
Returns MessagingError::MissingLocalAddress if local_address() was not called,
or a network error if binding to the local address fails.