libloading::os::unix

Function with_dlerror

Source
pub fn with_dlerror<T, F, Error>(
    closure: F,
    error: fn(_: &CStr) -> Error,
) -> Result<T, Option<Error>>
where F: FnOnce() -> Option<T>,
Expand description

Run code and handle errors reported by dlerror.

This function first executes the closure function containing calls to the functions that report their errors via dlerror. This closure may return either None or Some(*) to further affect operation of this function.

In case the closure returns None, with_dlerror inspects the dlerror. dlerror may decide to not provide any error description, in which case Err(None) is returned to the caller. Otherwise the error callback is invoked to allow inspection and conversion of the error message. The conversion result is returned as Err(Some(Error)).

If the operations that report their errors via dlerror were all successful, closure should return Some(T) instead. In this case dlerror is not inspected at all.

ยงNotes

The whole dlerror handling scheme is done via setting and querying some global state. For that reason it is not safe to use dynamic library loading in MT-capable environment at all. Only in POSIX 2008+TC1 a thread-local state was allowed for dlerror, making the dl* family of functions possibly MT-safe, depending on the implementation of dlerror.

In practice (as of 2020-04-01) most of the widely used targets use a thread-local for error state and have been doing so for a long time.