bp3d_os/module/library/
virtual.rs

1// Copyright (c) 2025, BlockProject 3D
2//
3// All rights reserved.
4//
5// Redistribution and use in source and binary forms, with or without modification,
6// are permitted provided that the following conditions are met:
7//
8//     * Redistributions of source code must retain the above copyright notice,
9//       this list of conditions and the following disclaimer.
10//     * Redistributions in binary form must reproduce the above copyright notice,
11//       this list of conditions and the following disclaimer in the documentation
12//       and/or other materials provided with the distribution.
13//     * Neither the name of BlockProject 3D nor the names of its contributors
14//       may be used to endorse or promote products derived from this software
15//       without specific prior written permission.
16//
17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
21// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29use crate::module::error::Error;
30use crate::module::library::symbol::Symbol;
31use crate::module::library::Library;
32use std::ffi::c_void;
33
34/// This represents a virtual library to be used in full statically-linked applications.
35#[derive(Copy, Clone)]
36pub struct VirtualLibrary {
37    name: &'static str,
38    symbols: &'static [(&'static str, *const c_void)],
39}
40
41unsafe impl Send for VirtualLibrary {}
42
43unsafe impl Sync for VirtualLibrary {}
44
45impl VirtualLibrary {
46    /// Creates a new [VirtualLibrary] from a name and an array of symbols.
47    ///
48    /// This function is const to allow statics declaration from build tool.
49    pub const fn new(
50        name: &'static str,
51        symbols: &'static [(&'static str, *const c_void)],
52    ) -> Self {
53        Self { name, symbols }
54    }
55
56    /// Returns the name of this [VirtualLibrary].
57    pub fn name(&self) -> &'static str {
58        self.name
59    }
60}
61
62impl Library for VirtualLibrary {
63    unsafe fn load_symbol<T>(
64        &self,
65        name: impl AsRef<str>,
66    ) -> crate::module::Result<Option<Symbol<'_, T>>> {
67        if name.as_ref().find('\0').is_some() {
68            return Err(Error::Null);
69        }
70        for (name1, symbol) in self.symbols {
71            if *name1 == name.as_ref() {
72                return Ok(Some(Symbol::from_raw(*symbol)));
73            }
74        }
75        Ok(None)
76    }
77}