Sling Academy
Home/Rust/Exploring unsafe Functions and Safe Wrappers in Rust

Exploring unsafe Functions and Safe Wrappers in Rust

Last updated: January 03, 2025

The Rust programming language is widely celebrated for its emphasis on safety, particularly in preventing memory-related errors that are rampant in languages like C and C++. By default, Rust's compiler checks help ensure the code is safe. However, there are moments in a Rust developer's journey where they need to interact directly with the system memory or perform operations considered unsafe. This is where Rust's unsafe keyword comes into play.

Understanding Unsafe Functions in Rust

In Rust, the unsafe keyword is used to designate blocks of code or functions that perform potentially dangerous operations, such as:

  • Dereferencing raw pointers
  • Calling unsafe functions or methods
  • Accessing mutable static variables
  • Implementing unsafe traits

The need for unsafe code typically arises when interfacing with other systems, such as hardware or operating system features, or when optimizing code for performance in ways that Rust's borrowing rules would otherwise prevent.

Let's look at a simple example of an unsafe function:

fn main() {
    let mut num: i32 = 5;
    let num_ptr: *mut i32 = &mut num;

    unsafe {
        *num_ptr = 10;
    }

    println!("Value of num is: {}", num);
}

In the example above, using raw pointers requires an unsafe block. Here, we successfully changed the value of num through its pointer.

Creating Safe Wrappers

While unsafe code blocks provide flexibility, they bypass the enforced safety guarantees that Rust offers. Therefore, a common practice is to encapsulate unsafe code within safe abstraction layers, creating safe wrappers.

Safe wrappers help minimize the scope of unsafety to specific, verifiable portions of code, maintaining the safety of the API exposed to the users of your code. Let’s take a look at an example to demonstrate a safe wrapper:

struct SafeBox {
    value: *mut T,
}

impl SafeBox {
    fn new(value: T) -> SafeBox {
        SafeBox {
            value: Box::into_raw(Box::new(value)),
        }
    }

    fn get(&self) -> &T {
        unsafe { &*self.value }
    }

    fn get_mut(&mut self) -> &mut T {
        unsafe { &mut *self.value }
    }
}

impl Drop for SafeBox {
    fn drop(&mut self) {
        unsafe {
            // Safety: we know we are the only owner here since SafeBox has exclusive ownership.
            Box::from_raw(self.value);
        }
    }
}

fn main() {
    let mut safe_box = SafeBox::new(42);
    println!("SafeBox contains: {}", safe_box.get());

    *safe_box.get_mut() = 10;
    println!("SafeBox contains: {}", safe_box.get());
}

In this code, we define a SafeBox struct that encapsulates the raw pointer manipulations inside its methods. The methods get and get_mut safely read and write to the value, respectively, while hiding the unsafe operations from the users of SafeBox.

Best Practices

Here are a few best practices when dealing with unsafe code in Rust:

  • Limit the unsafe blocks: Keep unsafe blocks as small as possible. Every line of code within them is harder to trust.
  • Document invariants clearly: It's crucial to note what has been "proven safe" through reasoning or validations when using unsafe code.
  • Perform rigorous testing: Always thoroughly test any code utilizing unsafe, ideally with tests that aim to stress the inferred safety assumptions.
  • Keep unsafe layers internal: When using modules or packages, prefer exposing safe interfaces and keeping unsafe code internal.

Letting Rust handle most safety checks reduces potential bugs, but knowing its unsafe capabilities allows for more advanced and fine-tuned control when needed. By creating safe abstractions over unsafe code, you can enjoy both performance and security in critical sections of your application.

Next Article: Early Returns in Rust for Cleaner Control Flow

Previous Article: Interfacing with C: extern "C" Functions in Rust

Series: Working with Functions in Rust

Rust

You May Also Like

  • E0557 in Rust: Feature Has Been Removed or Is Unavailable in the Stable Channel
  • Network Protocol Handling Concurrency in Rust with async/await
  • Using the anyhow and thiserror Crates for Better Rust Error Tests
  • Rust - Investigating partial moves when pattern matching on vector or HashMap elements
  • Rust - Handling nested or hierarchical HashMaps for complex data relationships
  • Rust - Combining multiple HashMaps by merging keys and values
  • Composing Functionality in Rust Through Multiple Trait Bounds
  • E0437 in Rust: Unexpected `#` in macro invocation or attribute
  • Integrating I/O and Networking in Rust’s Async Concurrency
  • E0178 in Rust: Conflicting implementations of the same trait for a type
  • Utilizing a Reactor Pattern in Rust for Event-Driven Architectures
  • Parallelizing CPU-Intensive Work with Rust’s rayon Crate
  • Managing WebSocket Connections in Rust for Real-Time Apps
  • Downloading Files in Rust via HTTP for CLI Tools
  • Mocking Network Calls in Rust Tests with the surf or reqwest Crates
  • Rust - Designing advanced concurrency abstractions using generic channels or locks
  • Managing code expansion in debug builds with heavy usage of generics in Rust
  • Implementing parse-from-string logic for generic numeric types in Rust
  • Rust.- Refining trait bounds at implementation time for more specialized behavior