Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Customizing bindings using annotations

The Rust bindings for a C++ declaration can be customized using an attribute macro from "support/annotations.h".

For instance:

  • A function can be marked unsafe in Rust, even if Crubit would otherwise assume it was safe, using CRUBIT_UNSAFE.
  • Missing bindings for an item can be treated as an error, instead of ignored, using CRUBIT_MUST_BIND.
  • An item can be given a different name in Rust using CRUBIT_RUST_NAME("rust_name_here").
  • A function or method can be given pub(crate) visibility in Rust, limiting its visibility to the generated crate (including custom srcs on rust_api_from_cpp), using CRUBIT_PUB_CRATE.

More information:

  • Dependency: //support:annotations
  • Include: #include "support/annotations.h"
  • Full API documentation: support/annotations.h

Example

Given the following C++ header:

CRUBIT_UNSAFE inline void SafeSignatureButAnnotatedUnsafe() {}

Crubit will generate the following bindings:

/// # Safety
///
/// The C++ function is explicitly annotated as unsafe. Ensure that its safety requirements are upheld.
#[inline(always)]
pub unsafe fn SafeSignatureButAnnotatedUnsafe() {
    unsafe { crate::detail::__rust_thunk___Z31SafeSignatureButAnnotatedUnsafev() }
}