diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:19:50 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:19:50 +0000 |
commit | 2e00214b3efbdfeefaa0fe9e8b8fd519de7adc35 (patch) | |
tree | d325add32978dbdc1db975a438b3a77d571b1ab8 /vendor/elsa/examples/mutable_arena.rs | |
parent | Releasing progress-linux version 1.68.2+dfsg1-1~progress7.99u1. (diff) | |
download | rustc-2e00214b3efbdfeefaa0fe9e8b8fd519de7adc35.tar.xz rustc-2e00214b3efbdfeefaa0fe9e8b8fd519de7adc35.zip |
Merging upstream version 1.69.0+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/elsa/examples/mutable_arena.rs')
-rw-r--r-- | vendor/elsa/examples/mutable_arena.rs | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/vendor/elsa/examples/mutable_arena.rs b/vendor/elsa/examples/mutable_arena.rs new file mode 100644 index 000000000..d5db2d331 --- /dev/null +++ b/vendor/elsa/examples/mutable_arena.rs @@ -0,0 +1,79 @@ +use elsa::FrozenVec; + +fn main() { + let arena = Arena::new(); + let lonely = arena.add_person("lonely", vec![]); + let best_friend = arena.add_person("best friend", vec![lonely]); + let threes_a_crowd = arena.add_person("threes a crowd", vec![lonely, best_friend]); + let rando = arena.add_person("rando", vec![]); + let _everyone = arena.add_person( + "follows everyone", + vec![rando, threes_a_crowd, lonely, best_friend], + ); + arena.dump(); +} + +struct Arena<'arena> { + people: FrozenVec<Box<Person<'arena>>>, +} + +struct Person<'arena> { + pub follows: FrozenVec<PersonRef<'arena>>, + pub reverse_follows: FrozenVec<PersonRef<'arena>>, + pub name: &'static str, +} + +type PersonRef<'arena> = &'arena Person<'arena>; + +impl<'arena> Arena<'arena> { + fn new() -> Arena<'arena> { + Arena { + people: FrozenVec::new(), + } + } + + fn add_person( + &'arena self, + name: &'static str, + follows: Vec<PersonRef<'arena>>, + ) -> PersonRef<'arena> { + let idx = self.people.len(); + self.people.push(Box::new(Person { + name, + follows: follows.into(), + reverse_follows: Default::default(), + })); + let me = &self.people[idx]; + for friend in &me.follows { + friend.reverse_follows.push(me) + } + me + } + + fn dump(&'arena self) { + for thing in &self.people { + println!("{} network:", thing.name); + println!("\tfollowing:"); + for friend in &thing.follows { + println!("\t\t{}", friend.name); + } + println!("\tfollowers:"); + for friend in &thing.reverse_follows { + println!("\t\t{}", friend.name); + } + } + } +} + +// Note that the following will cause the above code to stop compiling +// since non-eyepatched custom destructors can potentially +// read deallocated data. +// +// impl<'arena> Drop for Person<'arena> { +// fn drop(&mut self) { +// println!("goodbye {:?}", self.name); +// for friend in &self.follows { +// println!("\t\t{}", friend.name); +// } +// } +// } |