summaryrefslogtreecommitdiffstats
path: root/vendor/elsa/examples
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:19:50 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:19:50 +0000
commit2e00214b3efbdfeefaa0fe9e8b8fd519de7adc35 (patch)
treed325add32978dbdc1db975a438b3a77d571b1ab8 /vendor/elsa/examples
parentReleasing progress-linux version 1.68.2+dfsg1-1~progress7.99u1. (diff)
downloadrustc-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')
-rw-r--r--vendor/elsa/examples/arena.rs56
-rw-r--r--vendor/elsa/examples/fluentresource.rs50
-rw-r--r--vendor/elsa/examples/mutable_arena.rs79
-rw-r--r--vendor/elsa/examples/string_interner.rs61
-rw-r--r--vendor/elsa/examples/sync.rs26
5 files changed, 272 insertions, 0 deletions
diff --git a/vendor/elsa/examples/arena.rs b/vendor/elsa/examples/arena.rs
new file mode 100644
index 000000000..79913c2e7
--- /dev/null
+++ b/vendor/elsa/examples/arena.rs
@@ -0,0 +1,56 @@
+use elsa::FrozenVec;
+
+fn main() {
+ let arena = Arena::new();
+ let lonely = arena.add_thing("lonely", vec![]);
+ let best_friend = arena.add_thing("best friend", vec![lonely]);
+ let threes_a_crowd = arena.add_thing("threes a crowd", vec![lonely, best_friend]);
+ let rando = arena.add_thing("rando", vec![]);
+ let _facebook = arena.add_thing("facebook", vec![rando, threes_a_crowd, lonely, best_friend]);
+
+ assert!(cmp_ref(lonely, best_friend.friends[0]));
+ assert!(cmp_ref(best_friend, threes_a_crowd.friends[1]));
+ arena.dump();
+}
+
+struct Arena<'arena> {
+ things: FrozenVec<Box<Thing<'arena>>>,
+}
+
+struct Thing<'arena> {
+ pub friends: Vec<ThingRef<'arena>>,
+ pub name: &'static str,
+}
+
+type ThingRef<'arena> = &'arena Thing<'arena>;
+
+impl<'arena> Arena<'arena> {
+ fn new() -> Arena<'arena> {
+ Arena {
+ things: FrozenVec::new(),
+ }
+ }
+
+ fn add_thing(
+ &'arena self,
+ name: &'static str,
+ friends: Vec<ThingRef<'arena>>,
+ ) -> ThingRef<'arena> {
+ let idx = self.things.len();
+ self.things.push(Box::new(Thing { name, friends }));
+ &self.things[idx]
+ }
+
+ fn dump(&'arena self) {
+ for thing in &self.things {
+ println!("friends of {}:", thing.name);
+ for friend in &thing.friends {
+ println!("\t{}", friend.name);
+ }
+ }
+ }
+}
+
+fn cmp_ref<T>(x: &T, y: &T) -> bool {
+ x as *const T as usize == y as *const T as usize
+}
diff --git a/vendor/elsa/examples/fluentresource.rs b/vendor/elsa/examples/fluentresource.rs
new file mode 100644
index 000000000..dba4aaed8
--- /dev/null
+++ b/vendor/elsa/examples/fluentresource.rs
@@ -0,0 +1,50 @@
+use elsa::FrozenMap;
+
+/// Stores some parsed AST representation of the file
+#[derive(Debug)]
+pub struct FluentResource<'mgr>(&'mgr str);
+
+impl<'mgr> FluentResource<'mgr> {
+ pub fn new(s: &'mgr str) -> Self {
+ // very simple parse step
+ FluentResource(&s[0..1])
+ }
+}
+
+/// Stores loaded files and parsed ASTs
+///
+/// Parsed ASTs are zero-copy and
+/// contain references to the files
+pub struct ResourceManager<'mgr> {
+ strings: FrozenMap<String, String>,
+ resources: FrozenMap<String, Box<FluentResource<'mgr>>>,
+}
+
+impl<'mgr> ResourceManager<'mgr> {
+ pub fn new() -> Self {
+ ResourceManager {
+ strings: FrozenMap::new(),
+ resources: FrozenMap::new(),
+ }
+ }
+
+ pub fn get_resource(&'mgr self, path: &str) -> &'mgr FluentResource<'mgr> {
+ let strings = &self.strings;
+
+ if strings.get(path).is_some() {
+ return self.resources.get(path).unwrap();
+ } else {
+ // pretend to load a file
+ let string = format!("file for {}", path);
+ let val = self.strings.insert(path.to_string(), string);
+ let res = FluentResource::new(val);
+ self.resources.insert(path.to_string(), Box::new(res))
+ }
+ }
+}
+
+fn main() {
+ let manager = ResourceManager::new();
+ let resource = manager.get_resource("somefile.ftl");
+ println!("{:?}", resource);
+}
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);
+// }
+// }
+// }
diff --git a/vendor/elsa/examples/string_interner.rs b/vendor/elsa/examples/string_interner.rs
new file mode 100644
index 000000000..fd039f7ba
--- /dev/null
+++ b/vendor/elsa/examples/string_interner.rs
@@ -0,0 +1,61 @@
+use std::collections::BTreeSet;
+use std::convert::AsRef;
+
+use elsa::FrozenIndexSet;
+
+struct StringInterner {
+ set: FrozenIndexSet<String>,
+}
+
+impl StringInterner {
+ fn new() -> Self {
+ StringInterner {
+ set: FrozenIndexSet::new(),
+ }
+ }
+
+ fn get_or_intern<T>(&self, value: T) -> usize
+ where
+ T: AsRef<str>,
+ {
+ // TODO use Entry in case the standard Entry API gets improved
+ // (here to avoid premature allocation or double lookup)
+ self.set.insert_full(value.as_ref().to_string()).0
+ }
+
+ fn get<T>(&self, value: T) -> Option<usize>
+ where
+ T: AsRef<str>,
+ {
+ self.set.get_full(value.as_ref()).map(|(i, _r)| i)
+ }
+
+ fn resolve(&self, index: usize) -> Option<&str> {
+ self.set.get_index(index)
+ }
+}
+
+fn main() {
+ let interner = StringInterner::new();
+ let lonely = interner.get_or_intern("lonely");
+ let best_friend = interner.get_or_intern("best friend");
+ let threes_a_crowd = interner.get_or_intern("threes a crowd");
+ let rando = interner.get_or_intern("rando");
+ let _facebook = interner.get_or_intern("facebook");
+
+ let best_friend_2 = interner.get_or_intern("best friend");
+ let best_friend_3 = interner.get("best friend").unwrap();
+
+ let best_friend_ref = interner.resolve(best_friend).unwrap();
+
+ let mut set = BTreeSet::new();
+ set.insert(lonely);
+ set.insert(best_friend);
+ set.insert(threes_a_crowd);
+ set.insert(rando);
+ set.insert(best_friend_2);
+ assert_eq!(set.len(), 4);
+ assert_eq!(best_friend, best_friend_2);
+ assert_eq!(best_friend_2, best_friend_3);
+ assert_eq!(best_friend_ref, "best friend");
+}
diff --git a/vendor/elsa/examples/sync.rs b/vendor/elsa/examples/sync.rs
new file mode 100644
index 000000000..c6d9eb3cc
--- /dev/null
+++ b/vendor/elsa/examples/sync.rs
@@ -0,0 +1,26 @@
+use elsa::sync::*;
+
+use std::sync::Arc;
+use std::thread;
+use std::time::Duration;
+
+fn main() {
+ let a = Arc::new(FrozenMap::new());
+ for i in 1..10 {
+ let b = a.clone();
+ thread::spawn(move || {
+ b.insert(i, i.to_string());
+ thread::sleep(Duration::from_millis(300));
+ loop {
+ if let Some(opposite) = b.get(&(10 - i)) {
+ assert!(opposite.parse::<i32>().unwrap() == 10 - i);
+ break;
+ } else {
+ thread::sleep(Duration::from_millis(200));
+ }
+ }
+ });
+ }
+
+ thread::sleep(Duration::from_millis(1000));
+}