Skip to main content

ClientOrderIdEncoder

Struct ClientOrderIdEncoder 

Source
pub struct ClientOrderIdEncoder { /* private fields */ }
Expand description

Manages bidirectional mapping of ClientOrderId ↔ (client_id, client_metadata) for dYdX.

§Encoding Strategy

  1. Numeric IDs (e.g., “12345”): Encoded as (12345, 4) for backward compatibility
  2. O-format IDs (e.g., “O-20260131-174827-001-001-1”): Deterministically encoded
  3. Other formats: Sequential allocation with in-memory mapping

§Thread Safety

All operations are thread-safe using DashMap and AtomicU32.

Implementations§

Source§

impl ClientOrderIdEncoder

Source

pub fn new() -> Self

Creates a new encoder with counter starting at 1.

Source

pub fn encode( &self, id: ClientOrderId, ) -> Result<EncodedClientOrderId, EncoderError>

Encodes a ClientOrderId to (client_id, client_metadata) pair.

§Encoding Rules
  1. If already mapped in cache, returns existing encoded pair
  2. If numeric (e.g., “12345”), returns (12345, DEFAULT_RUST_CLIENT_METADATA)
  3. If O-format, deterministically encodes timestamp + identity bits
  4. Otherwise, allocates sequential ID for fallback
§Errors

Returns EncoderError::CounterOverflow if sequential counter exceeds safe limit. Returns EncoderError::ValueOverflow if O-format values exceed bit limits.

Source

pub fn decode( &self, client_id: u32, client_metadata: u32, ) -> Option<ClientOrderId>

Decodes (client_id, client_metadata) back to the original ClientOrderId.

§Decoding Rules
  1. If client_metadata == DEFAULT_RUST_CLIENT_METADATA (4): Return numeric string
  2. If client_metadata == SEQUENTIAL_METADATA_MARKER: Look up in sequential reverse mapping
  3. Otherwise: Decode as O-format using timestamp + identity bits

Returns None if decoding fails (e.g., sequential ID not in cache).

Source

pub fn get(&self, id: &ClientOrderId) -> Option<EncodedClientOrderId>

Gets the existing encoded pair without allocating a new one.

First checks the forward mapping (for updated/modified orders), then falls back to deterministic computation for O-format and numeric IDs.

Source

pub fn remove( &self, client_id: u32, client_metadata: u32, ) -> Option<ClientOrderId>

Removes the mapping for a given encoded pair.

Returns the original ClientOrderId if it was mapped. For deterministic formats, this is a no-op.

Source

pub fn remove_by_client_id(&self, client_id: u32) -> Option<ClientOrderId>

Legacy remove method for backward compatibility. Removes by client_id only, assumes DEFAULT_RUST_CLIENT_METADATA.

Source

pub fn current_counter(&self) -> u32

Returns the current counter value (for debugging/monitoring).

Source

pub fn len(&self) -> usize

Returns the number of non-deterministic mappings currently stored.

Source

pub fn is_empty(&self) -> bool

Returns true if no non-deterministic mappings are stored.

Trait Implementations§

Source§

impl Debug for ClientOrderIdEncoder

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for ClientOrderIdEncoder

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

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.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
§

impl<T> IntoRequest<T> for T

§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
§

impl<L> LayerExt<L> for L

§

fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>
where L: Layer<S>,

Applies the layer to a service and wraps it in [Layered].
§

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

§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] only if self and other return Action::Follow. Read more
§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

impl<T> Ungil for T
where T: Send,