Where can I read about how to write a safe API for unsafe code?

This page summarizes the projects mentioned and recommended in the original post on /r/rust

Our great sponsors
  • InfluxDB - Power Real-Time Data Analytics at Scale
  • WorkOS - The modern identity platform for B2B SaaS
  • SaaSHub - Software Alternatives and Reviews
  • bytemuck

    A crate for mucking around with piles of bytes

  • bytemuck uses a number of traits to provide safe APIs for bitcasts in cases where it's actually safe. This doesn't cover the full spectrum of safety in Rust (I imagine Send and Sync aren't too relevant here?), but if you can understand how this crate provides a safe API on top of a fair bit of unsafe, then you'll be well on your way here. (For extra credit, study zerocopy as well, which lives in a similar domain. Compare its use of unsafe and the functionality it provides with bytemuck.) I mention these crates because after a cursory look, they have decent comments describing safety obligations. So you have a decent chance of actually understanding the boundary between safe and unsafe here.

  • zerocopy

  • bytemuck uses a number of traits to provide safe APIs for bitcasts in cases where it's actually safe. This doesn't cover the full spectrum of safety in Rust (I imagine Send and Sync aren't too relevant here?), but if you can understand how this crate provides a safe API on top of a fair bit of unsafe, then you'll be well on your way here. (For extra credit, study zerocopy as well, which lives in a similar domain. Compare its use of unsafe and the functionality it provides with bytemuck.) I mention these crates because after a cursory look, they have decent comments describing safety obligations. So you have a decent chance of actually understanding the boundary between safe and unsafe here.

  • InfluxDB

    Power Real-Time Data Analytics at Scale. Get real-time insights from all types of time series data with InfluxDB. Ingest, query, and analyze billions of data points in real-time with unbounded cardinality.

    InfluxDB logo
  • crossbeam

    Tools for concurrent programming in Rust

  • Shooting from the hip, crossbeam might be a good candidate for understanding the thread safety aspects of Rust. I kind of feel like this is probably "too big" of a project if you're just learning, but I can't think of something smaller off the top of my head that would be suitable.

NOTE: The number of mentions on this list indicates mentions on common posts plus user suggested alternatives. Hence, a higher number means a more popular project.

Suggest a related project

Related posts