How to Wrap Unsafe Code in a Safe API

Wrap unsafe code in a safe API by isolating the unsafe block within a public function that guarantees safety invariants to callers.

Wrap unsafe code in a safe API by defining a public function that internally uses an unsafe block to perform the operation, ensuring the function's public interface guarantees safety invariants.

pub fn safe_operation() -> i32 {
    unsafe {
        // Unsafe logic here, e.g., dereferencing a raw pointer
        let ptr: *const i32 = &42;
        *ptr
    }
}

This pattern isolates the unsafe block within a single function, preventing callers from accessing the unsafe code directly while the compiler enforces that the function maintains memory safety.