How to declare a function without implementing it?

Viewed 1382

I'd like to compare the assembly of different implementations of functions but I don't want to implement certain functions, just declare them.

In Rust, forward declaration are typically unnecessary, due to the compiler not needing the functions in order to resolve them (unlike in C). However, is it possible to do something equivalent to a forward declaration?

3 Answers

If you declare your function as #[inline(never)] you will get a function call instruction to prevent further optimizations.

The main limitation is that your function must not be empty after optimizations, so it must have some side effect (thanks to @hellow that suggests using compiler_fence instead of println!).

For example, this code (godbolt):

pub fn test_loop(num: i32) {
    for _i in 0..num {
        dummy();
    }
}

#[inline(never)]
pub extern fn dummy() {
    use std::sync::atomic::*;
    compiler_fence(Ordering::Release);
}

Will produce the following assembly (with -O), that I think you need:

example::test_loop:
        push    r14
        push    rbx
        push    rax
        test    edi, edi
        jle     .LBB0_3
        mov     ebx, edi
        mov     r14, qword ptr [rip + example::dummy@GOTPCREL]
.LBB0_2:
        call    r14
        add     ebx, -1
        jne     .LBB0_2
.LBB0_3:
        add     rsp, 8
        pop     rbx
        pop     r14
        ret

plus the code for dummy() that is actually empty:

example::dummy:
        ret

It's not possible to forward-declare functions. There is only a single declaration for any given entity in Rust.

However, you can use the unimplemented!() and todo!() macros to quickly fill in the bodies of functions you don't want to implement (yet) for some reason. Both are basically aliases for panic!() with specific error messages.

Declare a trait and have your function and its signature in there. eg.

pub trait Summary {
  fn summarize(&self) -> String;
}
Related