【发布时间】:2019-08-11 13:48:35
【问题描述】:
我有一个队列 @987654323@ 特征,其中实现了 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()
}
}
结果不同,但没有运气。有没有办法轻松指定?
【问题讨论】:
标签: rust