Skip to main content

Sheriff

Struct Sheriff 

Source
pub struct Sheriff { /* private fields */ }
Expand description

A global registry for Cowboy instances

Implementations§

Source§

impl Sheriff

Source

pub fn register<K, T>(&self, key: K, cowboy: Cowboy<T>)
where K: Eq + Hash + Clone + Send + Sync + 'static, T: 'static + Send + Sync,

Register a Cowboy instance with a key

use cowboy::*;

let player = "Player".to_string().cowboy();
SHERIFF.register("player1", player);
Examples found in repository?
examples/basic.rs (line 19)
1fn main() {
2    use cowboy::*;
3
4    // Create a shared counter
5    let counter = 0.cowboy();
6    let counter_2 = counter.clone();
7
8    println!("Counter: {counter}");
9
10    // Modify the value
11    {
12        let mut value = counter.w();
13        *value += 1;
14    }
15
16    assert_eq!(counter, counter_2);
17
18    // Add counters to the global registry with different key types
19    SHERIFF.register("counter", counter.clone());
20    SHERIFF.register(42, counter.clone());
21
22    // Access from anywhere using different key types
23    let counter_1 = SHERIFF.get::<_, i32>("counter");
24    let counter_2 = SHERIFF.get::<_, i32>(42); // Note: not &42
25
26    *counter.w() += 1;
27    *counter_1.w() += 2;
28    *counter_2.w() += 3;
29
30    // All counters should have the same value since they're all clones of the same original counter
31    assert_eq!(counter_1, counter_2);
32    println!("Counter: {counter}");
33}
More examples
Hide additional examples
examples/sheriff.rs (line 34)
30fn main() {
31    println!("Demonstrating global access to Cowboy instances\n");
32
33    // Register players with different key types
34    SHERIFF.register("player1", Player::new().cowboy()); // String key
35    SHERIFF.register(PlayerId(2), Player::new().cowboy()); // Custom type key
36    SHERIFF.register(42, Player::new().cowboy()); // Integer key
37
38    // Modify players through the global registry
39    {
40        // String key
41        let player = SHERIFF.get::<_, Player>("player1");
42        player.w().name = "Gunslinger".to_string();
43        player.w().shoot();
44
45        // Custom type key
46        let player2 = SHERIFF.get::<_, Player>(PlayerId(2));
47        player2.w().name = "Sharpshooter".to_string();
48        player2.w().shoot();
49
50        // Integer key
51        let player3 = SHERIFF.get::<_, Player>(42);
52        player3.w().name = "Desperado".to_string();
53        player3.w().shoot();
54    }
55
56    // Create a thread that accesses the player with custom key
57    let handle = thread::spawn(move || {
58        // Access the player from anywhere
59        let player = SHERIFF.get::<_, Player>(PlayerId(2));
60        for _ in 1..=3 {
61            let mut player_write = player.w();
62            player_write.shoot();
63            thread::sleep(Duration::from_millis(100));
64        }
65    });
66
67    // Wait for the thread to complete
68    handle.join().unwrap();
69
70    // Access all players again from the main thread
71    println!("\nFinal player states:");
72
73    let player1 = SHERIFF.get::<_, Player>("player1");
74    let player1_read = player1.r();
75    println!("Player1 (String key):");
76    println!("  Name: {}", player1_read.name);
77    println!("  Score: {}", player1_read.score);
78
79    let player2 = SHERIFF.get::<_, Player>(PlayerId(2));
80    let player2_read = player2.r();
81    println!("Player2 (PlayerId key):");
82    println!("  Name: {}", player2_read.name);
83    println!("  Score: {}", player2_read.score);
84
85    let player3 = SHERIFF.get::<_, Player>(42);
86    let player3_read = player3.r();
87    println!("Player3 (Integer key):");
88    println!("  Name: {}", player3_read.name);
89    println!("  Score: {}", player3_read.score);
90}
Source

pub fn get<K, T>(&self, key: K) -> Cowboy<T>
where K: Eq + Hash + Send + Sync + 'static, T: 'static + Send + Sync,

Get a Cowboy instance by key

use cowboy::*;

// First register a value
let player = "Player".to_string().cowboy();
SHERIFF.register("player1", player);

// Then retrieve it
let retrieved: Cowboy<String> = SHERIFF.get("player1");
assert_eq!(*retrieved.read(), "Player");
Examples found in repository?
examples/basic.rs (line 23)
1fn main() {
2    use cowboy::*;
3
4    // Create a shared counter
5    let counter = 0.cowboy();
6    let counter_2 = counter.clone();
7
8    println!("Counter: {counter}");
9
10    // Modify the value
11    {
12        let mut value = counter.w();
13        *value += 1;
14    }
15
16    assert_eq!(counter, counter_2);
17
18    // Add counters to the global registry with different key types
19    SHERIFF.register("counter", counter.clone());
20    SHERIFF.register(42, counter.clone());
21
22    // Access from anywhere using different key types
23    let counter_1 = SHERIFF.get::<_, i32>("counter");
24    let counter_2 = SHERIFF.get::<_, i32>(42); // Note: not &42
25
26    *counter.w() += 1;
27    *counter_1.w() += 2;
28    *counter_2.w() += 3;
29
30    // All counters should have the same value since they're all clones of the same original counter
31    assert_eq!(counter_1, counter_2);
32    println!("Counter: {counter}");
33}
More examples
Hide additional examples
examples/sheriff.rs (line 41)
30fn main() {
31    println!("Demonstrating global access to Cowboy instances\n");
32
33    // Register players with different key types
34    SHERIFF.register("player1", Player::new().cowboy()); // String key
35    SHERIFF.register(PlayerId(2), Player::new().cowboy()); // Custom type key
36    SHERIFF.register(42, Player::new().cowboy()); // Integer key
37
38    // Modify players through the global registry
39    {
40        // String key
41        let player = SHERIFF.get::<_, Player>("player1");
42        player.w().name = "Gunslinger".to_string();
43        player.w().shoot();
44
45        // Custom type key
46        let player2 = SHERIFF.get::<_, Player>(PlayerId(2));
47        player2.w().name = "Sharpshooter".to_string();
48        player2.w().shoot();
49
50        // Integer key
51        let player3 = SHERIFF.get::<_, Player>(42);
52        player3.w().name = "Desperado".to_string();
53        player3.w().shoot();
54    }
55
56    // Create a thread that accesses the player with custom key
57    let handle = thread::spawn(move || {
58        // Access the player from anywhere
59        let player = SHERIFF.get::<_, Player>(PlayerId(2));
60        for _ in 1..=3 {
61            let mut player_write = player.w();
62            player_write.shoot();
63            thread::sleep(Duration::from_millis(100));
64        }
65    });
66
67    // Wait for the thread to complete
68    handle.join().unwrap();
69
70    // Access all players again from the main thread
71    println!("\nFinal player states:");
72
73    let player1 = SHERIFF.get::<_, Player>("player1");
74    let player1_read = player1.r();
75    println!("Player1 (String key):");
76    println!("  Name: {}", player1_read.name);
77    println!("  Score: {}", player1_read.score);
78
79    let player2 = SHERIFF.get::<_, Player>(PlayerId(2));
80    let player2_read = player2.r();
81    println!("Player2 (PlayerId key):");
82    println!("  Name: {}", player2_read.name);
83    println!("  Score: {}", player2_read.score);
84
85    let player3 = SHERIFF.get::<_, Player>(42);
86    let player3_read = player3.r();
87    println!("Player3 (Integer key):");
88    println!("  Name: {}", player3_read.name);
89    println!("  Score: {}", player3_read.score);
90}
Source

pub fn contains<K>(&self, key: &K) -> bool
where K: Eq + Hash + Clone + Send + Sync + 'static,

Check if a key is registered

use cowboy::*;

// Register a value
let player = "Player".to_string().cowboy();
SHERIFF.register("player1", player);

// Check if keys exist
assert!(SHERIFF.contains(&"player1"));
assert!(!SHERIFF.contains(&"player2"));
Source

pub fn remove<K>(&self, key: &K) -> bool
where K: Eq + Hash + Clone + Send + Sync + 'static,

Remove a registered Cowboy instance

use cowboy::*;

// Register a value
let player = "Player".to_string().cowboy();
SHERIFF.register("player1", player);

// Remove it
assert!(SHERIFF.remove(&"player1"));
assert!(!SHERIFF.contains(&"player1"));

// Trying to remove a non-existent key returns false
assert!(!SHERIFF.remove(&"player2"));

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoCowboy for T

Source§

fn cowboy(self) -> Cowboy<T>

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.