Skip to content

Commit ae834f9

Browse files
Added NSTDWindowsOptionalSharedLib.
1 parent 918420e commit ae834f9

File tree

3 files changed

+17
-17
lines changed

3 files changed

+17
-17
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ nstd_os_windows_alloc = [
3636
"nstd_core", "nstd_os", "windows-sys/Win32_Foundation", "windows-sys/Win32_System_Memory"
3737
]
3838
nstd_os_windows_shared_lib = [
39-
"nstd_os", "windows-sys/Win32_Foundation", "windows-sys/Win32_System_LibraryLoader"
39+
"nstd_core", "nstd_os", "windows-sys/Win32_Foundation", "windows-sys/Win32_System_LibraryLoader"
4040
]
4141
nstd_shared_lib = ["libloading", "nstd_core", "std"]
4242
nstd_shared_ptr = ["nstd_alloc", "nstd_core"]

include/nstd/os/windows/shared_lib.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#ifndef NSTD_OS_WINDOWS_SHARED_LIB_H
22
#define NSTD_OS_WINDOWS_SHARED_LIB_H
3+
#include "../../core/optional.h"
34
#include "../../nstd.h"
45

56
/// A handle to a loaded library.
@@ -8,6 +9,9 @@ typedef struct {
89
NSTDInt handle;
910
} NSTDWindowsSharedLib;
1011

12+
/// An optional (possibly null) shared Windows library handle.
13+
NSTDOptional(NSTDWindowsSharedLib) NSTDWindowsOptionalSharedLib;
14+
1115
/// Loads a shared library/module by name.
1216
///
1317
/// # Parameters:
@@ -16,17 +20,13 @@ typedef struct {
1620
///
1721
/// # Returns
1822
///
19-
/// `NSTDWindowsSharedLib lib` - A handle to the shared library.
20-
///
21-
/// # Panics
22-
///
23-
/// Panics if getting a handle to the shared library fails.
23+
/// `NSTDWindowsOptionalSharedLib lib` - A handle to the shared library.
2424
///
2525
/// # Safety
2626
///
2727
/// See
2828
/// <https://docs.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-loadlibrarya>.
29-
NSTDAPI NSTDWindowsSharedLib nstd_os_windows_shared_lib_load(const NSTDChar *name);
29+
NSTDAPI NSTDWindowsOptionalSharedLib nstd_os_windows_shared_lib_load(const NSTDChar *name);
3030

3131
/// Gets a pointer to a function or static variable in a dynamically loaded library by symbol name.
3232
///

src/os/windows/shared_lib.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//! Shared library/module access for Windows.
2-
use crate::{NSTDAny, NSTDAnyMut, NSTDChar, NSTDInt};
2+
use crate::{core::optional::NSTDOptional, NSTDAny, NSTDAnyMut, NSTDChar, NSTDInt};
33
use windows_sys::Win32::System::LibraryLoader::{FreeLibrary, GetProcAddress, LoadLibraryA};
44

55
/// A handle to a loaded library.
@@ -17,6 +17,9 @@ impl Drop for NSTDWindowsSharedLib {
1717
}
1818
}
1919

20+
/// An optional (possibly null) shared Windows library handle.
21+
pub type NSTDWindowsOptionalSharedLib = NSTDOptional<NSTDWindowsSharedLib>;
22+
2023
/// Loads a shared library/module by name.
2124
///
2225
/// # Parameters:
@@ -25,11 +28,7 @@ impl Drop for NSTDWindowsSharedLib {
2528
///
2629
/// # Returns
2730
///
28-
/// `NSTDWindowsSharedLib lib` - A handle to the shared library.
29-
///
30-
/// # Panics
31-
///
32-
/// Panics if getting a handle to the shared library fails.
31+
/// `NSTDWindowsOptionalSharedLib lib` - A handle to the shared library.
3332
///
3433
/// # Safety
3534
///
@@ -39,10 +38,11 @@ impl Drop for NSTDWindowsSharedLib {
3938
#[cfg_attr(feature = "clib", no_mangle)]
4039
pub unsafe extern "C" fn nstd_os_windows_shared_lib_load(
4140
name: *const NSTDChar,
42-
) -> NSTDWindowsSharedLib {
43-
let handle = LoadLibraryA(name.cast());
44-
assert!(handle != 0);
45-
NSTDWindowsSharedLib { handle }
41+
) -> NSTDWindowsOptionalSharedLib {
42+
match LoadLibraryA(name.cast()) {
43+
0 => NSTDOptional::None,
44+
handle => NSTDOptional::Some(NSTDWindowsSharedLib { handle }),
45+
}
4646
}
4747

4848
/// Gets a pointer to a function or static variable in a dynamically loaded library by symbol name.

0 commit comments

Comments
 (0)