pub trait ResourceBuilder<T> {
    fn new() -> Self;
    fn build<'life0, 'life1, 'async_trait>(
        self,
        factory: &'life0 mut dyn Factory,
        runtime: &'life1 Runtime
    ) -> Pin<Box<dyn Future<Output = Result<T, Error>> + Send + 'async_trait>>
    where
        'life0: 'async_trait,
        'life1: 'async_trait,
        Self: 'async_trait
; }
Expand description

Used to get resources of type T from factories.

This is mainly meant for consumption by our code generator and should generally not be called by users.

Creating your own managed resource

You may want to create your own managed resource by implementing this trait for some builder B to construct resource T. Factory can be used to provision resources on shuttle’s servers if your resource will need any.

The biggest thing to look out for is that your resource object might panic when it crosses the boundary between the shuttle’s backend runtime and the runtime of services. These resources should be created on the passed in runtime for this trait to prevent these panics.

Your resource will be available on a shuttle_service::main function as follow:

#[shuttle_service::main]
async fn my_service([custom_resource_crate::namespace::B] custom_resource: T)
    -> shuttle_service::ShuttleAxum {}

Here custom_resource_crate::namespace is the crate and namespace to a builder B that implements ResourceBuilder to create resource T.

Example

pub struct Builder {
    name: String,
}

pub struct Resource {
    name: String,
}

impl Builder {
    /// Name to give resource
    pub fn name(self, name: &str) -> Self {
        self.name = name.to_string();

        self
    }
}

#[async_trait]
impl ResourceBuilder<Resource> for Builder {
    fn new() -> Self {
        Self {
            name: String::new(),
        }
    }

    async fn build(
        self,
        factory: &mut dyn Factory,
        _runtime: &Runtime,
    ) -> Result<Resource, shuttle_service::Error> {
        Ok(Resource { name: self.name })
    }
}

Then using this resource in a service:

#[shuttle_service::main]
async fn my_service(
    [custom_resource_crate::Builder(name = "John")] resource: custom_resource_crate::Resource
)
    -> shuttle_service::ShuttleAxum {}

Required Methods

Implementors