I was starting to write a RFC to add some markers for const fn
, but then I realized that was only the tip of the iceberg, and that I might as well try to address the problem more generally.
The rust standard library documentation does a great job indicating in what rust version features, types, methods have been added, so that developers can make informed decisions whether they can use APIs based on the baseline rust version they are targetting for compatibility.
However, it stops there. Developers can read when a particular method was added, but not when the particular signature they are looking at was.
For example, the nightly documentation for Vec::new
says its signature is:
pub const fn new() -> Vec<T>
meaning one can do:
static mut MYVEC : Vec<u8> = Vec::new();
… but it doesn’t tell that const
was added in 1.27.0. So this code is not valid before that version.
But const is not the only change that can happen to APIs: they can be made more generic than they were when they were first introduced, thanks to features like generics or default trait parameters.
I don’t have concrete examples to give, but consider the following:
fn foo(s: &str) { ... }
fn bar<T>(t: Vec<T>) { ... }
The following are backwards compatible changes:
fn foo<T: AsRef<str>>(s: T) {... }
fn bar<T, A: Alloc = Global>(t: Vec<T, A>) { ... }
I’m sure there are already plenty of examples of such changes in the standard library. One that comes to mind is how the PartialEq
trait changed from being trait PartialEq
to being trait PartialEq<Rhs: ?Sized = Self>
, but that was before 1.0.
When I first thinking about this, with only const in mind, I was thinking we could extend the #[stable]
attribute to add the information, but considering the latter examples, I’m not sure anymore. Maybe a documentation convention about how to document those changes is what’s needed here. But maybe the since
in the #[stable]
attribute should be changed whenever an API is modified, too. So that the documentation clearly indicates that the documented API is available in that version, and then the documentation itself can go on to say that simpler versions of the API were available in older versions.