Skip to main content

Transport

Trait Transport 

Source
pub trait Transport {
    // Required method
    fn send(&self, request: Request) -> Result<Response, TransportError>;
}
Expand description

What a host implements to carry a request to a server.

send is synchronous. It makes one attempt and does not retry. It answers the response that came back, whatever its status, or a TransportError when none came back.

The trait is object safe and used as &dyn Transport. It takes &self so one transport serves many sends; a host whose client needs mutation uses interior mutability. It has no Send or Sync supertrait: a host that shares one transport across threads names the bound where it holds it, as &(dyn Transport + Sync), and a single-threaded host’s transport needs neither.

use statifier::basichttp::{Request, Response, Transport, TransportError};

struct InMemory;

impl Transport for InMemory {
    fn send(&self, request: Request) -> Result<Response, TransportError> {
        if request.address.starts_with("http://") {
            Ok(Response::new(200, Vec::new(), request.body))
        } else {
            Err(TransportError::new("not an http address"))
        }
    }
}

let transport: &dyn Transport = &InMemory;
let request = Request::new("POST", "http://localhost:8080/s1", Vec::new(), "e.go");
assert_eq!(transport.send(request).unwrap().status, 200);

Required Methods§

Source

fn send(&self, request: Request) -> Result<Response, TransportError>

Sends one request and answers the response that came back, or the error when none did.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§