Skip to main content

resource_reader

Function resource_reader 

Source
pub fn resource_reader<F, Fut>(read: F) -> ResourceHandler
where F: Fn(RequestContext, String) -> Fut + Send + Sync + 'static, Fut: Future<Output = Vec<ResourceContents>> + Send + 'static,
Expand description

Adapts a resource reader into the ResourceHandler a server stores.

Takes the URI by value rather than by reference. A reference here would put a lifetime on the bound, and a higher-ranked for<'uri> in a where clause also binds the return type: an async fn returns a different future for each 'uri, so no single Fut can satisfy it and the bound rejects every async reader. Owned arguments leave the future type independent of any lifetime. #[server]’s expansion wraps a reference-taking reader into this shape, so #[resource] readers are still written as fn(&str).

The reader takes and returns owned values, so the boxed future is 'static and does not borrow the request.

§Examples

use protocol_droid::application::server::builder::ResourceHandler;
use protocol_droid::application::server::context::RequestContext;
use protocol_droid::application::server::read::resource_reader;
use protocol_droid::domain::ir::ResourceContents;

// The context is required rather than optional: it carries who is asking,
// and a reader that could not see it would serve every caller as whoever
// opened the connection.
async fn readme(_context: RequestContext, uri: String) -> Vec<ResourceContents> {
    vec![ResourceContents::text(uri, "the body")]
}

let handler: ResourceHandler = resource_reader(readme);
assert!(std::sync::Arc::strong_count(&handler) >= 1);