vcg_auction/traits.rs
1use std::ops::{Add, Sub};
2
3use num_traits::Zero;
4
5/// Trait with methods that take two references of the same type and returns the
6/// result of addition or subtraction. Used as a trait bound for [`Bid`]
7/// associated types.
8pub trait AddSubSelf {
9 fn add(&self, other: &Self) -> Self;
10 fn sub(&self, other: &Self) -> Self;
11}
12
13/// Implementation for any type that implements Add and Sub with itself, such as
14/// integer and floating point primitives.
15impl<T> AddSubSelf for T
16where
17 T: Add<Output = T> + Sub<Output = T> + Copy,
18{
19 fn add(&self, other: &Self) -> Self {
20 *self + *other
21 }
22 fn sub(&self, other: &Self) -> Self {
23 *self - *other
24 }
25}
26
27/// Trait for a bid that can be auctioned.
28pub trait Bid {
29 /// Identifier for bidders. E.g. strings or integers.
30 type Name: Eq;
31 /// Bid value. E.g. integers. Floats can be used with a type wrapper for
32 /// [`Ord`]. See the tests for an example.
33 type Value: Ord + AddSubSelf + Zero;
34 /// Identifier for items. E.g. strings or integers.
35 type Item: Eq;
36 /// Quantity of an item. E.g. integers or floats.
37 type Quantity: PartialOrd + AddSubSelf + Zero + Clone;
38
39 /// Get the name of the bidder.
40 fn bidder_name(&self) -> &Self::Name;
41 /// Get the value of the bid.
42 fn bid_value(&self) -> &Self::Value;
43 /// Get the items that are bid on, and their quantities
44 fn bid_items(&self) -> &[(Self::Item, Self::Quantity)];
45}