Skip to main content

StorageBuilder

Struct StorageBuilder 

Source
pub struct StorageBuilder<LockingPolicyFactory>
where LockingPolicyFactory: LockingPolicyFactory,
{ /* private fields */ }
Expand description

Builder for Storage instances configured with a specific LockingPolicyFactory.

§Purpose

Trading policies built on openpit typically maintain in-memory state (reserved margin, position tracking, rate-limit counters, …) that must be thread-safe in multi-threaded embeddings yet have zero overhead in single-threaded ones. Rather than implementing ad hoc synchronization in every policy, the recommended pattern is:

  1. Accept a &StorageBuilder<Factory> obtained from an engine builder at policy construction time.
  2. Call create_for_bound_key for each internal data table.
  3. Operate on the resulting Storage instances exclusively through Storage::with and Storage::with_mut.

The synchronization mode is then entirely determined by the engine’s synchronization mode, not by the policy. Switching from no-sync to fully-synchronized execution only requires changing the engine builder’s sync mode - the policy logic is untouched.

§Lifetime discipline

StorageBuilder is intentionally not Clone. The intended usage is for the engine builder to own it during the application’s initialization phase. Application code passes SyncedEngineBuilder::storage_builder by shared reference to every policy that needs to create storages.

Storing a StorageBuilder inside a policy — even by value through unsafe code — is a misuse: the builder is only meaningful during initialization, after which the storages it produced are already live and the builder itself carries no additional information.

§Examples

use openpit::Engine;

let engine_builder = Engine::builder::<(), (), ()>().full_sync();
let users = engine_builder.storage_builder().create_for_bound_key::<u64, String>();
let orders = engine_builder.storage_builder().create_for_bound_key::<u64, Vec<u8>>();
// `users` and `orders` are unrelated storages; locking one does
// not affect the other.
users.with_mut(1, || "alice".to_string(), |_, _| {});
orders.with_mut(42, Vec::new, |_, _| {});

Implementations§

Source§

impl<LockingPolicyFactory> StorageBuilder<LockingPolicyFactory>
where LockingPolicyFactory: LockingPolicyFactory,

Source

pub fn create_for_bound_key<Key, Value>( &self, ) -> Storage<Key, Value, LockingPolicyFactory::Policy>
where LockingPolicyFactory: CreateStorageFor<Key>,

Creates an empty storage configured by this builder’s policy factory.

Each call obtains a fresh LockingPolicy instance from the factory; the resulting storage’s locks are independent of any other storage created by this builder.

The factory must declare it supports Key by implementing CreateStorageFor<Key>. All built-in factories ship with the appropriate impls.

Source

pub fn create_shared<Key, Value>( &self, ) -> LockingPolicyFactory::Shared<Storage<Key, Value, LockingPolicyFactory::Policy>>
where LockingPolicyFactory: CreateStorageFor<Key>,

Creates an empty storage wrapped in a LockingPolicyFactory::Shared handle.

Equivalent to Factory::new_shared(self.create_for_bound_key::<Key, Value>()). Use this when the storage needs to be shared across clones of the same policy.

Source

pub fn create_with_capacity<Key, Value>( &self, capacity: usize, ) -> Storage<Key, Value, LockingPolicyFactory::Policy>
where LockingPolicyFactory: CreateStorageFor<Key>,

Creates an empty storage with an initial capacity hint for the underlying map.

Otherwise identical to Self::create_for_bound_key; useful when the rough number of entries is known up front and the caller wants to avoid rehashing during the warm-up phase.

Auto Trait Implementations§

§

impl<LockingPolicyFactory> Freeze for StorageBuilder<LockingPolicyFactory>
where LockingPolicyFactory: Freeze,

§

impl<LockingPolicyFactory> RefUnwindSafe for StorageBuilder<LockingPolicyFactory>
where LockingPolicyFactory: RefUnwindSafe,

§

impl<LockingPolicyFactory> Send for StorageBuilder<LockingPolicyFactory>
where LockingPolicyFactory: Send,

§

impl<LockingPolicyFactory> Sync for StorageBuilder<LockingPolicyFactory>
where LockingPolicyFactory: Sync,

§

impl<LockingPolicyFactory> Unpin for StorageBuilder<LockingPolicyFactory>
where LockingPolicyFactory: Unpin,

§

impl<LockingPolicyFactory> UnsafeUnpin for StorageBuilder<LockingPolicyFactory>
where LockingPolicyFactory: UnsafeUnpin,

§

impl<LockingPolicyFactory> UnwindSafe for StorageBuilder<LockingPolicyFactory>
where LockingPolicyFactory: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.