У меня есть черта очереди Strategy
с реализациями для Monotonic
и LastTick
, параметризованными для типа, который я хочу вставить:
struct Monotonic<T> {
items: Vec<T>,
}
struct LastTick<T> {
items: Vec<T>,
}
struct SetDelta;
trait Strategy<T> {
type T;
fn enqueue(&mut self, v: T);
fn has_pending(&self) -> bool;
}
impl<T> Strategy<T> for Monotonic<T> {
type T = Self;
fn enqueue(&mut self, v: T) {
self.items.push(v);
}
fn has_pending(&self) -> bool {
!self.items.is_empty()
}
}
impl<T> Strategy<T> for LastTick<T> {
type T = Self;
fn enqueue(&mut self, v: T) {
self.items.push(v);
}
fn has_pending(&self) -> bool {
!self.items.is_empty()
}
}
impl<T> Strategy<T> for LastTick<T> {
type T = Self;
fn enqueue(&mut self, v: T) {
self.items.push(v);
}
fn has_pending(&self) -> bool {
!self.items.is_empty()
}
}
impl<T> LastTick<T> {
fn new() -> Self {
LastTick { items: Vec::new() }
}
}
impl<T> Monotonic<T> {
fn new() -> Self {
Monotonic { items: Vec::new() }
}
}
#[test]
fn monotonic_scalar_queue() {
let mut a = Monotonic::<f64>::new();
a.enqueue(123.4);
assert!(a.has_pending());
}
#[test]
fn monotonic_list_queue() {
let mut a = Monotonic::<[f64; 3]>::new();
a.enqueue([123.4, 345.8, 324.1]);
assert!(a.has_pending());
}
#[test]
fn monotonic_tuple_queue() {
let mut a = Monotonic::<(f64, String, u32)>::new();
a.enqueue((123.4, "hello".into(), 324));
assert!(a.has_pending());
}
Вышеописанное работает нормально.Я хочу сохранить тот же интерфейс для HashSet
, где поведение немного отличается.
#[test]
fn monotonic_set_queue() {
let mut a = Monotonic::<HashSet<f64>>::new();
// I want to insert a f64 and implement the logic of the hashset in
// the implementation, but it expects a new HashSet
a.enqueue(123.4);
assert!(a.has_pending());
}
Я пытался
impl<T> Strategy<T> for Monotonic<HashSet<f64>> {
type T = Self;
fn enqueue(&mut self, v: T) {
self.items.push(v);
}
fn has_pending(&self) -> bool {
!self.items.is_empty()
}
}
, а также
impl Strategy<f64> for Monotonic<f64> {
type T = HashSet<f64>;
fn enqueue(&mut self, v: f64) {
self.items.push(v);
}
fn has_pending(&self) -> bool {
!self.items.is_empty()
}
}
с разными результатами, но не повезло.Есть ли способ указать это легко?