dragostis
dragostis

Reputation: 2662

Is there any way to use private functions in public macros in Rust?

There is a variadic C function that I cannot call outside of a macro. This macro is public which it should be, but the C function with variadic arguments should not be visible.

Is there any way to only make it visible for inside of the macro? Or maybe a way to keep the function outside of the docs?

Upvotes: 9

Views: 2516

Answers (1)

DK.
DK.

Reputation: 59065

The only thing you can do is hide such "internal" symbols such that they do not appear in the documentation. For example:

#[macro_export]
macro_rules! custom_abort {
    ($($args:tt)*) => {
        match format!($($args)*) {
            msg => $crate::custom_abort__(&msg)
        }
    };
}

/// This is an implementation detail and *should not* be called directly!
#[doc(hidden)]
pub fn custom_abort__(msg: &str) -> ! {
    use std::io::Write;
    let _ = writeln!(std::io::stderr(), "{}", msg);
    std::process::exit(1);
}

As you might expect, this absolutely does not prevent someone from calling custom_abort__ directly. But really, if someone ignores warnings in the comments and does so anyway, feel free to laugh at them when their code breaks.

Upvotes: 13

Related Questions