Reputation: 871
Is there a way to simplify the type signature here
pub struct OrderBook<T: PriceLevel, U: PriceLadder<T>> {
levels: U,
phantom: PhantomData<T>,
}
such that
type HashBook = OrderBook<LinkedHashLevel, HashmapLadder<LinkedHashLevel>>;
can be written as follows?
type HashBook = OrderBook<LinkedHashLevel, HashmapLadder>;
I'm aware of associated types but want to be able to use a single price ladder with different price level implementations.
Complete compilable code below...
use std::marker::PhantomData;
use std::collections::HashMap;
pub struct OrderBook<T: PriceLevel, U: PriceLadder<T>> {
levels: U,
phantom: PhantomData<T>,
}
impl<T: PriceLevel, U: PriceLadder<T>> OrderBook<T, U> {}
type HashBook = OrderBook<LinkedHashLevel, HashmapLadder<LinkedHashLevel>>;
pub trait PriceLevel {
fn new() -> Self where Self: Sized;
}
pub trait PriceLadder<T: PriceLevel> {
fn new() -> Self;
}
pub struct LinkedHashLevel {
}
impl PriceLevel for LinkedHashLevel {
fn new() -> Self {
LinkedHashLevel {}
}
}
pub struct HashmapLadder<T: PriceLevel> {
levels: HashMap<u64, T>,
}
impl<T: PriceLevel> PriceLadder<T> for HashmapLadder<T> {
fn new() -> Self {
HashmapLadder { levels: HashMap::new() }
}
}
fn main() {}
Upvotes: 1
Views: 67
Reputation: 11177
I think that is not necessary to change the type signature. Considering what you sad:
I'm aware of associated types but want to be able to use a single price ladder with different implementations of price level.
Maybe it is enough to create a type type alias parametrized by the price level implementation and with a fixed price ladder:
type HashBook<Level> = OrderBook<Level, HashmapLadder<Level>>;
Upvotes: 2