scoped
Scoped operations and callback patterns
This module provides high-level APIs for scoped operations that guarantee setup and cleanup. Built on top of the RAII patterns but providing more ergonomic APIs for common use cases.
Key Features
Section titled “Key Features”- Scoped Callbacks: Execute code with guaranteed setup/cleanup
- Builder Pattern: Fluent APIs for complex scoped operations
- Exception Safety: Cleanup happens even during panics
- Return Values: Scoped operations can return values from callbacks
- Composable: Scoped operations can be nested and combined
Traits
Section titled “Traits”ScopedCallback
Section titled “ScopedCallback”Trait for scoped callback operations
This trait provides a standard interface for types that support scoped operations with automatic cleanup.
Required / Provided Methods
fn with_scope<F, R>(self, f: F) -> Rwhere F: FnOnceExecute a function with scoped access to the resource
IntoScoped
Section titled “IntoScoped”Helper trait for making types support scoped operations
Implement this trait to add scoped operation support to your types.
Associated Types
type Resource—
Required / Provided Methods
fn into_scoped(self) -> <Self as ?>::ResourceConvert into a scoped resource
fn scoped<F, R>(self, f: F) -> Rwhere Self: Sized, F: FnOnceExecute a scoped operation with this resource
Structs
Section titled “Structs”ScopedBuilder
Section titled “ScopedBuilder”Builder for creating complex scoped operations
This builder allows you to compose multiple scoped operations with different setup/cleanup phases.
Example
Section titled “Example”use foundation_utils::scoped::ScopedBuilder;
let result = ScopedBuilder::new() .with_resource("resource1", |r| println!("cleanup: {}", r)) .with_resource("resource2", |r| println!("cleanup: {}", r)) .execute(|resources| { format!("used {} resources", resources.len()) });Methods
fn new() -> SelfCreate a new scoped builder
with_resource
Section titled “with_resource”fn with_resource<F>(self, resource: T, cleanup: F) -> Selfwhere F: FnOnce + ?Add a resource with cleanup to the scoped operation
Arguments
Section titled “Arguments”resource: The resource to managecleanup: Function to clean up the resource when the scope ends
execute
Section titled “execute”fn execute<F, R>(self, work: F) -> Rwhere F: FnOnceExecute the scoped operation with all resources
All cleanup functions will be called in reverse order (LIFO) even if the work function panics.
Functions
Section titled “Functions”with_context
Section titled “with_context”fn with_context<T, F, R>(context: T, f: F) -> Rwhere F: FnOnceExecute a function with a scoped context
This is the most common scoped operation pattern. It sets up a context, executes a function with that context, and automatically cleans up.
Type Parameters
Section titled “Type Parameters”T: The type of contextF: The type of function to executeR: The return type of the function
Arguments
Section titled “Arguments”context: The context value to set upf: The function to execute with the context
Example
Section titled “Example”use foundation_utils::scoped::with_context;
let result = with_context("my_context", |ctx| { println!("Working with context: {}", ctx); 42});assert_eq!(result, 42);with_setup_cleanup
Section titled “with_setup_cleanup”fn with_setup_cleanup<S, W, C, T, R>(setup: S, work: W, cleanup: C) -> Rwhere S: FnOnce, W: FnOnce, C: FnOnceExecute a function with scoped setup and cleanup
This pattern provides explicit setup and cleanup functions that are guaranteed to be called even if the work function panics.
Type Parameters
Section titled “Type Parameters”S: Setup function typeW: Work function typeC: Cleanup function typeT: Resource type returned by setupR: Return type of work function
Arguments
Section titled “Arguments”setup: Function to set up the resourcework: Function to do work with the resourcecleanup: Function to clean up the resource
Example
Section titled “Example”use foundation_utils::scoped::with_setup_cleanup;
let result = with_setup_cleanup( || "resource", // Setup |resource| format!("used: {}", resource), // Work |resource| println!("cleanup: {}", resource) // Cleanup);with_optional_scope
Section titled “with_optional_scope”fn with_optional_scope<S, W, C, T, R>(condition: bool, setup: S, work: W, cleanup: C) -> Rwhere S: FnOnce, W: FnOnce, C: FnOnceExecute a function with optional scoped setup and cleanup
This is useful when setup/cleanup is conditional based on runtime conditions.
Example
Section titled “Example”use foundation_utils::scoped::with_optional_scope;
let should_setup = true;let result = with_optional_scope( should_setup, || "resource", // Setup (only if condition is true) |resource| format!("used: {:?}", resource), // Work |resource| println!("cleanup: {}", resource) // Cleanup);with_multiple_contexts
Section titled “with_multiple_contexts”fn with_multiple_contexts<T, S, W, C, R>(contexts: Vec<T>, setup: S, work: W, cleanup: C) -> Rwhere T: Clone, S: Fn, W: FnOnce, C: FnScoped operation that automatically manages multiple contexts
This is a convenience function for managing multiple contexts that need to be set up and torn down together.
Example
Section titled “Example”use foundation_utils::scoped::with_multiple_contexts;
let contexts = vec!["ctx1", "ctx2", "ctx3"];let result = with_multiple_contexts( contexts, |ctx| println!("setup: {}", ctx), |contexts| { println!("Working with {} contexts", contexts.len()); 42 }, |ctx| println!("cleanup: {}", ctx));with_error_scope
Section titled “with_error_scope”fn with_error_scope<S, W, C, T, R, E>(setup: S, work: W, cleanup: C) -> Result<R, E>where S: FnOnce, W: FnOnce, C: FnOnceConvenience function for scope-aware error handling
This function allows you to handle errors within a scoped operation while still ensuring cleanup happens.
Example
Section titled “Example”use foundation_utils::scoped::with_error_scope;
let result = with_error_scope( || Ok("resource"), |resource| { // Work that might fail if resource.len() > 0 { Ok(format!("processed: {}", resource)) } else { Err("empty resource") } }, |resource| println!("cleanup: {}", resource));