So I have a relatively contrived toy example.
I've fiddled with this such that my ego has sufficiently depleted to the point where I'm asking myself the deep questions like, "is this even possible without interior mutability?" IM lives in a spot in Rust I haven't had to play around with very much. Would love if anyone would recommend a blog post on the topic
Back to the problem at hand, I have a pair of toy tuple structs, one of which is just a a vector with ownership of the other.
Should I be modifying my wrapping to look like
struct OwnsOffer(Vec<RefCell<Offer>>);
,
or is there something dumb I'm overlooking?
struct Offer(u64, bool);
impl PartialEq for Offer {
fn eq(&self, other: &Offer) -> bool {
self.0 == other.0
}
}
// these trait bounds are satisfied just so I can use max on an iterator, SKIP TO ...
impl PartialOrd for Offer {
fn partial_cmp(&self, other: &Offer) -> Option<std::cmp::Ordering> {
if self.0 > other.0 {
Some(std::cmp::Ordering::Greater)
} else if self.0 == other.0 {
Some(std::cmp::Ordering::Equal)
} else {
Some(std::cmp::Ordering::Less)
}
}
}
impl Eq for Offer {}
impl Ord for Offer {
fn cmp(&self, other: &Offer) -> std::cmp::Ordering {
self.partial_cmp(other).unwrap()
}
}
struct OwnsOffer(Vec<Offer>);
// SKIP TO HERE
impl OwnsOffer {
pub fn push_new_offer(&mut self, offer: Offer) {
self.0.push(offer);
}
pub fn get_highest_offer(&self) -> Option<&Offer> {
self.0.iter().max()
}
pub fn accept_highest_offer(&mut self) {
let offer = self.get_highest_offer();
match offer {
None => panic!("accept highest active offer called on no active offers"), // I'm aware this is silly
Some(offer) => offer.1 = true,
}
}
}