pub struct Resource<T, S> { /* private fields */ }Expand description
A resource that enforces acquisition and release through the type system
§Type Parameters
T- The underlying resource typeS- The current state of the resource (phantom type)
§Example
use eventcore::resource::{Resource, states, ResourceManager};
use std::sync::Arc;
// Example with database pool resource (requires postgres feature)
// This would typically be used with eventcore-postgres crate:
// ```rust,ignore
// use eventcore::resource::database::{DatabaseResourceManager, DatabasePool};
// use sqlx::PgPool;
//
// let pool = Arc::new(PgPool::connect("postgres://localhost/mydb").await?);
// let manager = DatabaseResourceManager::new(pool);
// let db_resource = manager.acquire_pool().await?;
// let result = db_resource.execute_query("SELECT 1").await?;
// let _released = db_resource.release()?;
// ```
// Example with a custom resource type
struct MyResource {
data: String,
}
// Implement ResourceManager for your type
struct MyResourceManager;
#[async_trait::async_trait]
impl ResourceManager<MyResource> for MyResourceManager {
async fn acquire() -> Result<Resource<MyResource, states::Acquired>, eventcore::resource::ResourceError> {
// In practice, you'd acquire from a pool or create the resource
let resource = Self::create_initializing(MyResource { data: "example".to_string() });
Ok(resource.mark_acquired())
}
}
// Use the resource manager to acquire a resource
let resource = MyResourceManager::acquire().await?;
// Access the inner data (only possible in Acquired state)
let data = resource.get();
println!("Resource data: {}", data.data);
// Transition to released state
let _released: Resource<MyResource, states::Released> = resource.release()?;
// released resource cannot be used anymore (compile-time guarantee)
Implementations§
Source§impl<T> Resource<Arc<Mutex<T>>, Acquired>
Extension methods for mutex resources
impl<T> Resource<Arc<Mutex<T>>, Acquired>
Extension methods for mutex resources
Sourcepub fn lock(&self) -> ResourceResult<MutexGuardResource<'_, T>>
pub fn lock(&self) -> ResourceResult<MutexGuardResource<'_, T>>
Acquire the mutex lock
Returns a guard resource that enforces lock is held
Sourcepub fn try_lock(&self) -> ResourceResult<Option<MutexGuardResource<'_, T>>>
pub fn try_lock(&self) -> ResourceResult<Option<MutexGuardResource<'_, T>>>
Try to acquire the mutex lock without blocking
Returns None if the lock is currently held
Source§impl<T, S> Resource<T, S>
impl<T, S> Resource<T, S>
Sourcepub const fn get(&self) -> &Twhere
S: IsAcquired,
pub const fn get(&self) -> &Twhere
S: IsAcquired,
Get a reference to the underlying resource
Only available when resource is acquired
Sourcepub fn get_mut(&mut self) -> &mut Twhere
S: IsAcquired,
pub fn get_mut(&mut self) -> &mut Twhere
S: IsAcquired,
Get a mutable reference to the underlying resource
Only available when resource is acquired
Sourcepub fn into_inner(self) -> Twhere
S: IsAcquired,
pub fn into_inner(self) -> Twhere
S: IsAcquired,
Consume the resource and return the inner value
Only available when resource is acquired
Source§impl<T> Resource<T, Initializing>
State transitions for resources
impl<T> Resource<T, Initializing>
State transitions for resources
Sourcepub fn mark_acquired(self) -> Resource<T, Acquired>
pub fn mark_acquired(self) -> Resource<T, Acquired>
Transition from initializing to acquired state
This represents successful resource acquisition
Sourcepub fn mark_failed(self) -> Resource<T, Failed>
pub fn mark_failed(self) -> Resource<T, Failed>
Transition from initializing to failed state
This represents failed resource acquisition
Source§impl<T> Resource<T, Acquired>
impl<T> Resource<T, Acquired>
Sourcepub fn release(self) -> ResourceResult<Resource<T, Released>>
pub fn release(self) -> ResourceResult<Resource<T, Released>>
Release the resource, transitioning to released state
After release, the resource cannot be used anymore
Sourcepub fn mark_failed(self) -> Resource<T, Failed>
pub fn mark_failed(self) -> Resource<T, Failed>
Mark resource as failed due to an error
Failed resources can be recovered or released
Source§impl<T> Resource<T, Failed>
impl<T> Resource<T, Failed>
Sourcepub fn recover(self) -> ResourceResult<Resource<T, Acquired>>
pub fn recover(self) -> ResourceResult<Resource<T, Acquired>>
Attempt to recover a failed resource
If successful, transitions back to acquired state
Sourcepub fn release(self) -> ResourceResult<Resource<T, Released>>
pub fn release(self) -> ResourceResult<Resource<T, Released>>
Release a failed resource
This allows cleanup even when the resource is in a failed state
Trait Implementations§
Source§impl<T> ResourceExt<T, Acquired> for Resource<T, Acquired>
impl<T> ResourceExt<T, Acquired> for Resource<T, Acquired>
Source§fn managed(self, resource_type: &str) -> ManagedResource<T, Acquired>
fn managed(self, resource_type: &str) -> ManagedResource<T, Acquired>
Source§fn scoped(self) -> ResourceScope<T>
fn scoped(self) -> ResourceScope<T>
Source§fn with_timeout(self, timeout: Duration) -> TimedResourceGuard<T>where
T: Send + 'static,
fn with_timeout(self, timeout: Duration) -> TimedResourceGuard<T>where
T: Send + 'static,
Auto Trait Implementations§
impl<T, S> Freeze for Resource<T, S>where
T: Freeze,
impl<T, S> RefUnwindSafe for Resource<T, S>where
T: RefUnwindSafe,
S: RefUnwindSafe,
impl<T, S> Send for Resource<T, S>
impl<T, S> Sync for Resource<T, S>
impl<T, S> Unpin for Resource<T, S>
impl<T, S> UnwindSafe for Resource<T, S>where
T: UnwindSafe,
S: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<ES> EventStoreResourceExt<ES> for ES
impl<ES> EventStoreResourceExt<ES> for ES
Source§fn into_resource(self) -> Resource<ES, Acquired>
fn into_resource(self) -> Resource<ES, Acquired>
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<D> OwoColorize for D
impl<D> OwoColorize for D
Source§fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
Source§fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
Source§fn black(&self) -> FgColorDisplay<'_, Black, Self>
fn black(&self) -> FgColorDisplay<'_, Black, Self>
Source§fn on_black(&self) -> BgColorDisplay<'_, Black, Self>
fn on_black(&self) -> BgColorDisplay<'_, Black, Self>
Source§fn red(&self) -> FgColorDisplay<'_, Red, Self>
fn red(&self) -> FgColorDisplay<'_, Red, Self>
Source§fn on_red(&self) -> BgColorDisplay<'_, Red, Self>
fn on_red(&self) -> BgColorDisplay<'_, Red, Self>
Source§fn green(&self) -> FgColorDisplay<'_, Green, Self>
fn green(&self) -> FgColorDisplay<'_, Green, Self>
Source§fn on_green(&self) -> BgColorDisplay<'_, Green, Self>
fn on_green(&self) -> BgColorDisplay<'_, Green, Self>
Source§fn yellow(&self) -> FgColorDisplay<'_, Yellow, Self>
fn yellow(&self) -> FgColorDisplay<'_, Yellow, Self>
Source§fn on_yellow(&self) -> BgColorDisplay<'_, Yellow, Self>
fn on_yellow(&self) -> BgColorDisplay<'_, Yellow, Self>
Source§fn blue(&self) -> FgColorDisplay<'_, Blue, Self>
fn blue(&self) -> FgColorDisplay<'_, Blue, Self>
Source§fn on_blue(&self) -> BgColorDisplay<'_, Blue, Self>
fn on_blue(&self) -> BgColorDisplay<'_, Blue, Self>
Source§fn magenta(&self) -> FgColorDisplay<'_, Magenta, Self>
fn magenta(&self) -> FgColorDisplay<'_, Magenta, Self>
Source§fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>
fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>
Source§fn purple(&self) -> FgColorDisplay<'_, Magenta, Self>
fn purple(&self) -> FgColorDisplay<'_, Magenta, Self>
Source§fn on_purple(&self) -> BgColorDisplay<'_, Magenta, Self>
fn on_purple(&self) -> BgColorDisplay<'_, Magenta, Self>
Source§fn cyan(&self) -> FgColorDisplay<'_, Cyan, Self>
fn cyan(&self) -> FgColorDisplay<'_, Cyan, Self>
Source§fn on_cyan(&self) -> BgColorDisplay<'_, Cyan, Self>
fn on_cyan(&self) -> BgColorDisplay<'_, Cyan, Self>
Source§fn white(&self) -> FgColorDisplay<'_, White, Self>
fn white(&self) -> FgColorDisplay<'_, White, Self>
Source§fn on_white(&self) -> BgColorDisplay<'_, White, Self>
fn on_white(&self) -> BgColorDisplay<'_, White, Self>
Source§fn default_color(&self) -> FgColorDisplay<'_, Default, Self>
fn default_color(&self) -> FgColorDisplay<'_, Default, Self>
Source§fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>
fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>
Source§fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>
fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>
Source§fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>
fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>
Source§fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>
fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>
Source§fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>
fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>
Source§fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>
fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>
Source§fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>
fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>
Source§fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>
fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>
Source§fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>
fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>
Source§fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>
fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>
Source§fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>
fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>
Source§fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
Source§fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
Source§fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
Source§fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
Source§fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>
fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>
Source§fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>
fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>
Source§fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>
fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>
Source§fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>
fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>
Source§fn bold(&self) -> BoldDisplay<'_, Self>
fn bold(&self) -> BoldDisplay<'_, Self>
Source§fn dimmed(&self) -> DimDisplay<'_, Self>
fn dimmed(&self) -> DimDisplay<'_, Self>
Source§fn italic(&self) -> ItalicDisplay<'_, Self>
fn italic(&self) -> ItalicDisplay<'_, Self>
Source§fn underline(&self) -> UnderlineDisplay<'_, Self>
fn underline(&self) -> UnderlineDisplay<'_, Self>
Source§fn blink(&self) -> BlinkDisplay<'_, Self>
fn blink(&self) -> BlinkDisplay<'_, Self>
Source§fn blink_fast(&self) -> BlinkFastDisplay<'_, Self>
fn blink_fast(&self) -> BlinkFastDisplay<'_, Self>
Source§fn reversed(&self) -> ReversedDisplay<'_, Self>
fn reversed(&self) -> ReversedDisplay<'_, Self>
Source§fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>
fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>
Source§fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
OwoColorize::fg or
a color-specific method, such as OwoColorize::green, Read moreSource§fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
OwoColorize::bg or
a color-specific method, such as OwoColorize::on_yellow, Read more