Created
March 26, 2022 18:18
-
-
Save mxgrey/9cd738fab6d2396222062fb94c83c2c5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use sorted_vec::SortedSet; | |
use std::collections::BTreeSet; | |
#[derive(Debug)] | |
pub struct Mystruct { | |
value: u64, | |
id: std::string::String | |
} | |
impl std::cmp::PartialEq for Mystruct { | |
fn eq(&self, other: &Self) -> bool { | |
return self.value == other.value; | |
} | |
fn ne(&self, other: &Self) -> bool { | |
return self.value != other.value; | |
} | |
} | |
impl std::cmp::Eq for Mystruct { } | |
impl std::cmp::PartialOrd for Mystruct { | |
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> { | |
return self.value.partial_cmp(&other.value); | |
} | |
} | |
impl std::cmp::Ord for Mystruct { | |
fn cmp(&self, other: &Self) -> std::cmp::Ordering { | |
return self.value.cmp(&other.value); | |
} | |
} | |
impl Mystruct { | |
fn addr (&self) { | |
println!("addr ({}) {:p}", self.id, self); | |
} | |
} | |
impl Drop for Mystruct { | |
fn drop (&mut self) { | |
println!("drop ({}) {:p}", self.id, self); | |
} | |
} | |
fn main() { | |
println!("main..."); | |
println!("s..."); | |
let mut s = SortedSet::new(); | |
s.insert (Mystruct {value: 5, id: "A".to_string()}); | |
println!("initial s[0]: ({}) {:p}", s[0].id, &s[0]); | |
s[0].addr(); | |
s.insert (Mystruct {value: 5, id: "B".to_string()}); | |
println!("final s[0]: ({}) {:p}", s[0].id, &s[0]); | |
println!("b..."); | |
let mut b = BTreeSet::new(); | |
b.insert (Mystruct {value: 5, id: "A".to_string()}); | |
let b_0 = b.iter().next().unwrap(); | |
println!("initial b[0]: ({}) {:p}", b_0.id, b_0); | |
b.iter().next().unwrap().addr(); | |
b.insert (Mystruct {value: 5, id: "B".to_string()}); | |
let b_0 = b.iter().next().unwrap(); | |
println!("final b[0]: ({}) {:p}", b_0.id, b_0); | |
println!("...main"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment