Debian Codegraph @

src/slot.rs

raw symbols SCIP
1 /*
2 * Copyright 2024 Andrey Kutejko <[email protected]>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
16 *
17 * For more details see the file COPYING.
18 */
19
20 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::{
21 cellrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/::RefCellrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/RefCell#,
22 collectionsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/::HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#,
23 rcrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc rc/::Rcrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc rc/Rc#,
24 syncrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/::atomicrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/::{AtomicU32rust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/AtomicU32#, Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/Ordering#},
25 };
26
27 pub type Listenerrust-analyzer cargo gnome-robots 50.0.0 slot/Listener#<T> = Rcrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc rc/Rc#<dyn Fnrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/Fn#(&T)>;
28
29 pub struct Slotrust-analyzer cargo gnome-robots 50.0.0 slot/Slot#<T> {
30 idrust-analyzer cargo gnome-robots 50.0.0 slot/Slot#id.: u32,
31 listenersrust-analyzer cargo gnome-robots 50.0.0 slot/Slot#listeners.: RefCellrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/RefCell#<HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#<u32, Listenerrust-analyzer cargo gnome-robots 50.0.0 slot/Listener#<T>>>,
32 }
33
34 pub struct ListenerIdrust-analyzer cargo gnome-robots 50.0.0 slot/ListenerId#(u32, u32);
35
36 impl<T> Slotrust-analyzer cargo gnome-robots 50.0.0 slot/Slot#<T> {
37 pub fn newrust-analyzer cargo gnome-robots 50.0.0 slot/impl#[`Slot<T>`]new().() -> Selfrust-analyzer cargo gnome-robots 50.0.0 slot/impl#[`Slot<T>`] {
38 Selfrust-analyzer cargo gnome-robots 50.0.0 slot/impl#[`Slot<T>`] {
39 idrust-analyzer cargo gnome-robots 50.0.0 slot/Slot#id.: new_idrust-analyzer cargo gnome-robots 50.0.0 slot/new_id().(),
40 listenersrust-analyzer cargo gnome-robots 50.0.0 slot/Slot#listeners.: RefCellrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/RefCell#::newrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`RefCell<T>`]new().(HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V>`]new().()),
41 }
42 }
43
44 pub fn connectrust-analyzer cargo gnome-robots 50.0.0 slot/impl#[`Slot<T>`]connect().(&self, listener: impl Fnrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/Fn#(&T) + 'static) -> ListenerIdrust-analyzer cargo gnome-robots 50.0.0 slot/ListenerId# {
45 let listener_id = new_idrust-analyzer cargo gnome-robots 50.0.0 slot/new_id().();
46 self.listenersrust-analyzer cargo gnome-robots 50.0.0 slot/Slot#listeners.
47 .borrow_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`RefCell<T>`]borrow_mut().()
48 .insertrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`]insert().(listener_id, Rcrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc rc/Rc#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc rc/impl#[`Rc<T>`]new().(listener));
49 ListenerIdrust-analyzer cargo gnome-robots 50.0.0 slot/ListenerId#(self.idrust-analyzer cargo gnome-robots 50.0.0 slot/Slot#id., listener_id)
50 }
51
52 pub fn disconnectrust-analyzer cargo gnome-robots 50.0.0 slot/impl#[`Slot<T>`]disconnect().(&self, listener_id: ListenerIdrust-analyzer cargo gnome-robots 50.0.0 slot/ListenerId#) {
53 if listener_id.0rust-analyzer cargo gnome-robots 50.0.0 slot/ListenerId#0. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialEq<Self>`]eq().self.idrust-analyzer cargo gnome-robots 50.0.0 slot/Slot#id. {
54 self.listenersrust-analyzer cargo gnome-robots 50.0.0 slot/Slot#listeners..borrow_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`RefCell<T>`]borrow_mut().().removerust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`]remove().(&listener_id.1rust-analyzer cargo gnome-robots 50.0.0 slot/ListenerId#1.);
55 } else {
56 eprintlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/eprintln!!("WARN: Cannot disconnect a listener. Wrong slot id.");
57 }
58 }
59
60 pub fn emitrust-analyzer cargo gnome-robots 50.0.0 slot/impl#[`Slot<T>`]emit().(&self, param: T) {
61 for listener in self.listenersrust-analyzer cargo gnome-robots 50.0.0 slot/Slot#listeners..borrowrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`RefCell<T>`]borrow().().valuesrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`]values().().clonedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#cloned().() {
62 (listener)(&param);
63 }
64 }
65 }
66
67 fn new_idrust-analyzer cargo gnome-robots 50.0.0 slot/new_id().() -> u32 {
68 static LAST_IDrust-analyzer cargo gnome-robots 50.0.0 slot/LAST_ID.: AtomicU32rust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/AtomicU32# = AtomicU32rust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/AtomicU32#::newrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/impl#[AtomicU32]new().(1);
69 LAST_IDrust-analyzer cargo gnome-robots 50.0.0 slot/LAST_ID..fetch_addrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/impl#[AtomicU32]fetch_add().(1, Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/Ordering#::SeqCstrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/Ordering#SeqCst#)
70 }