I have a function that is capable of being implemented as a const
:
#![feature(const_fn)]
// My crate would have:
const fn very_complicated_logic(a: u8, b: u8) -> u8 {
a * b
}
// The caller would have:
const ANSWER: u8 = very_complicated_logic(1, 2);
fn main() {}
I'd like to continue to support stable Rust where it's not possible to define such functions. These stable consumers would not be able to use the function in a const
or static
, but should be able to use the function in other contexts:
// My crate would have:
fn very_complicated_logic(a: u8, b: u8) -> u8 {
a * b
}
// The caller would have:
fn main() {
let answer: u8 = very_complicated_logic(1, 2);
}
How can I conditionally compile my code so that adventurous users of my crate could enable const fn
support, stable users would still be able to use my code, and I don't have to write every function twice?
The same question should apply to the other modifiers of a function, but I'm not sure of concrete cases where these modifiers would change based on some condition:
default
unsafe
extern
pub
(and other visibility modifiers)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…