Skip to content

Commit 41e08aa

Browse files
BD103alice-i-cecilejames7132
authored andcommitted
Document all members of bevy_dynamic_plugin (bevyengine#12029)
# Objective - Some members of `bevy_dynamic_plugin` are not documented. - Part of bevyengine#3492. ## Solution - Add documentation to members missing it in `bevy_dynamic_plugin`. - Update existing documentation for clarity and formatting. --- ## Changelog - Completely document `bevy_dynamic_plugin`. --------- Co-authored-by: Alice Cecile <[email protected]> Co-authored-by: James Liu <[email protected]>
1 parent fe0d05f commit 41e08aa

File tree

2 files changed

+35
-10
lines changed

2 files changed

+35
-10
lines changed

crates/bevy_dynamic_plugin/src/lib.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,23 @@
1-
// FIXME(3492): remove once docs are ready
2-
#![allow(missing_docs)]
1+
//! Bevy's dynamic plugin loading functionality.
2+
//!
3+
//! This crate allows loading dynamic libraries (`.dylib`, `.so`) that export a single
4+
//! [`Plugin`](bevy_app::Plugin). For usage, see [`dynamically_load_plugin`].
5+
//!
6+
//! Note that dynamic linking and loading is inherently unsafe because it allows executing foreign
7+
//! code. Additionally, Rust does not have a stable ABI and may produce
8+
//! incompatible libraries across Rust versions, or even subsequent compilations. This will not work
9+
//! well in scenarios such as modding, but can work if the dynamic plugins and the main app are
10+
//! built at the same time, such as with Downloadable Content (DLC) packs.
11+
//!
12+
//! You may be interested in these safer alternatives:
13+
//!
14+
//! - [Bevy Assets - Scripting]: Scripting and modding libraries for Bevy
15+
//! - [Bevy Assets - Development tools]: Hot reloading and other development functionality
16+
//! - [`stabby`]: Stable Rust ABI
17+
//!
18+
//! [Bevy Assets - Scripting]: https://bevyengine.org/assets/#scripting
19+
//! [Bevy Assets - Development tools]: https://bevyengine.org/assets/#development-tools
20+
//! [`stabby`]: https://github.com/ZettaScaleLabs/stabby
321
422
mod loader;
523

crates/bevy_dynamic_plugin/src/loader.rs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ use bevy_app::{App, CreatePlugin, Plugin};
77
/// Errors that can occur when loading a dynamic plugin
88
#[derive(Debug, Error)]
99
pub enum DynamicPluginLoadError {
10+
/// An error occurred when loading a dynamic library.
1011
#[error("cannot load library for dynamic plugin: {0}")]
1112
Library(#[source] libloading::Error),
13+
/// An error occurred when loading a library without a valid Bevy plugin.
1214
#[error("dynamic library does not contain a valid Bevy dynamic plugin")]
1315
Plugin(#[source] libloading::Error),
1416
}
@@ -18,22 +20,22 @@ pub enum DynamicPluginLoadError {
1820
///
1921
/// # Safety
2022
///
21-
/// The specified plugin must be linked against the exact same libbevy.so as this program.
23+
/// The specified plugin must be linked against the exact same `libbevy.so` as this program.
2224
/// In addition the `_bevy_create_plugin` symbol must not be manually created, but instead created
2325
/// by deriving `DynamicPlugin` on a unit struct implementing [`Plugin`].
2426
///
25-
/// Dynamically loading plugins is orchestrated through dynamic linking. When linking against foreign
26-
/// code, initialization routines may be run (as well as termination routines when the program exits).
27-
/// The caller of this function is responsible for ensuring these routines are sound. For more
28-
/// information, please see the safety section of [`libloading::Library::new`].
27+
/// Dynamically loading plugins is orchestrated through dynamic linking. When linking against
28+
/// foreign code, initialization routines may be run (as well as termination routines when the
29+
/// program exits). The caller of this function is responsible for ensuring these routines are
30+
/// sound. For more information, please see the safety section of [`libloading::Library::new`].
2931
pub unsafe fn dynamically_load_plugin<P: AsRef<OsStr>>(
3032
path: P,
3133
) -> Result<(Library, Box<dyn Plugin>), DynamicPluginLoadError> {
3234
// SAFETY: Caller must follow the safety requirements of Library::new.
3335
let lib = unsafe { Library::new(path).map_err(DynamicPluginLoadError::Library)? };
3436

35-
// SAFETY: Loaded plugins are not allowed to specify `_bevy_create_plugin` symbol manually, but must
36-
// instead automatically generate it through `DynamicPlugin`.
37+
// SAFETY: Loaded plugins are not allowed to specify `_bevy_create_plugin` symbol manually, but
38+
// must instead automatically generate it through `DynamicPlugin`.
3739
let func: Symbol<CreatePlugin> = unsafe {
3840
lib.get(b"_bevy_create_plugin")
3941
.map_err(DynamicPluginLoadError::Plugin)?
@@ -46,10 +48,15 @@ pub unsafe fn dynamically_load_plugin<P: AsRef<OsStr>>(
4648
Ok((lib, plugin))
4749
}
4850

51+
/// An extension trait for [`App`] that allows loading dynamic plugins.
4952
pub trait DynamicPluginExt {
53+
/// Dynamically links a plugin at the given path, registering the plugin.
54+
///
55+
/// For more details, see [`dynamically_load_plugin`].
56+
///
5057
/// # Safety
5158
///
52-
/// Same as [`dynamically_load_plugin`].
59+
/// See [`dynamically_load_plugin`]'s safety section.
5360
unsafe fn load_plugin<P: AsRef<OsStr>>(&mut self, path: P) -> &mut Self;
5461
}
5562

0 commit comments

Comments
 (0)