Loads the dynamic library at `path` with `RTLD_LAZY` binding. Returns a handle that keeps the library mapped until it is closed or dropped. # Errors Returns [`LoadError`] if the library cannot be loaded.
(path: &CStr)
| 100 | /// |
| 101 | /// Returns [`LoadError`] if the library cannot be loaded. |
| 102 | pub fn open(path: &CStr) -> Result<Self, LoadError> { |
| 103 | // SAFETY: `path` is a valid, null-terminated C string. |
| 104 | let ptr = unsafe { dlopen(path.as_ptr(), RTLD_LAZY) }; |
| 105 | let Some(ptr) = NonNull::new(ptr) else { |
| 106 | // SAFETY: `dlerror` is guaranteed to return a valid C string |
| 107 | let message = unsafe { CStr::from_ptr(dlerror()).to_owned() }; |
| 108 | return Err(LoadError { message }); |
| 109 | }; |
| 110 | |
| 111 | Ok(Self(Some(ptr))) |
| 112 | } |
| 113 | |
| 114 | /// Resolves a named symbol from the loaded library. |
| 115 | /// |