Skip to main content

bevy_utils/
lib.rs

1#![cfg_attr(docsrs, feature(doc_cfg))]
2#![doc(
3    html_logo_url = "https://bevy.org/assets/icon.png",
4    html_favicon_url = "https://bevy.org/assets/icon.png"
5)]
6#![no_std]
7
8//! General utilities for first-party [Bevy] engine crates.
9//!
10//! [Bevy]: https://bevy.org/
11
12/// Configuration information for this crate.
13pub mod cfg {
14    pub(crate) use bevy_platform::cfg::*;
15
16    pub use bevy_platform::cfg::{alloc, std};
17
18    define_alias! {
19        #[cfg(feature = "parallel")] => {
20            /// Indicates the `Parallel` type is available.
21            parallel
22        }
23        #[cfg(feature = "buffered_channel")] => {
24            /// Indicates the `BufferedChannel` type is available.
25            buffered_channel
26        }
27    }
28}
29
30cfg::std! {
31    extern crate std;
32}
33
34cfg::alloc! {
35    extern crate alloc;
36
37    mod map;
38    pub use map::*;
39}
40
41cfg::parallel! {
42    mod parallel_queue;
43    pub use parallel_queue::*;
44}
45
46cfg::buffered_channel! {
47    mod buffered_channel;
48    pub use buffered_channel::*;
49}
50
51/// The utilities prelude.
52///
53/// This includes the most common types in this crate, re-exported for your convenience.
54pub mod prelude {
55    pub use crate::debug_info::DebugName;
56    pub use crate::default;
57    pub use disqualified::ShortName;
58}
59
60mod atomic_id;
61mod bloom_filter;
62pub use bloom_filter::*;
63mod debug_info;
64mod default;
65mod once;
66
67#[doc(hidden)]
68pub use once::OnceFlag;
69
70pub use debug_info::DebugName;
71pub use default::default;
72
73use core::mem::ManuallyDrop;
74
75/// A type which calls a function when dropped.
76/// This can be used to ensure that cleanup code is run even in case of a panic.
77///
78/// Note that this only works for panics that [unwind](https://doc.rust-lang.org/nomicon/unwinding.html)
79/// -- any code within `OnDrop` will be skipped if a panic does not unwind.
80/// In most cases, this will just work.
81///
82/// # Examples
83///
84/// ```
85/// # use bevy_utils::OnDrop;
86/// # fn test_panic(do_panic: bool, log: impl FnOnce(&str)) {
87/// // This will print a message when the variable `_catch` gets dropped,
88/// // even if a panic occurs before we reach the end of this scope.
89/// // This is similar to a `try ... catch` block in languages such as C++.
90/// let _catch = OnDrop::new(|| log("Oops, a panic occurred and this function didn't complete!"));
91///
92/// // Some code that may panic...
93/// // ...
94/// # if do_panic { panic!() }
95///
96/// // Make sure the message only gets printed if a panic occurs.
97/// // If we remove this line, then the message will be printed regardless of whether a panic occurs
98/// // -- similar to a `try ... finally` block.
99/// core::mem::forget(_catch);
100/// # }
101/// #
102/// # test_panic(false, |_| unreachable!());
103/// # let mut did_log = false;
104/// # std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
105/// #   test_panic(true, |_| did_log = true);
106/// # }));
107/// # assert!(did_log);
108/// ```
109pub struct OnDrop<F: FnOnce()> {
110    callback: ManuallyDrop<F>,
111}
112
113impl<F: FnOnce()> OnDrop<F> {
114    /// Returns an object that will invoke the specified callback when dropped.
115    pub fn new(callback: F) -> Self {
116        Self {
117            callback: ManuallyDrop::new(callback),
118        }
119    }
120}
121
122impl<F: FnOnce()> Drop for OnDrop<F> {
123    fn drop(&mut self) {
124        #![expect(
125            unsafe_code,
126            reason = "Taking from a ManuallyDrop requires unsafe code."
127        )]
128        // SAFETY: We may move out of `self`, since this instance can never be observed after it's dropped.
129        let callback = unsafe { ManuallyDrop::take(&mut self.callback) };
130        callback();
131    }
132}