Skip to content

Commit 24cc294

Browse files
committed
Add docs and doc comment for unstable APIs
1 parent f4ce8c0 commit 24cc294

File tree

3 files changed

+40
-6
lines changed

3 files changed

+40
-6
lines changed

crates/backend/src/util.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -158,15 +158,17 @@ impl<T: Hash> fmt::Display for ShortHash<T> {
158158
}
159159

160160

161-
/// Create a syn attribute for `#[cfg(web_sys_unstable_apis)]`.
162-
pub fn unstable_api_attr() -> syn::Attribute {
163-
syn::parse_quote!( #[cfg(web_sys_unstable_apis)] )
161+
/// Create syn attribute for `#[cfg(web_sys_unstable_apis)]` and a doc comment.
162+
pub fn unstable_api_attrs() -> proc_macro2::TokenStream {
163+
quote::quote!(
164+
#[cfg(web_sys_unstable_apis)]
165+
#[doc = "\n\n*This API is unstable and requires `cfg=web_sys_unstable_apis` to be activated, as [described in the `wasm-bindgen` guide](https://rustwasm.github.io/docs/wasm-bindgen/web-sys/unstable-apis.html)*"]
166+
)
164167
}
165168

166-
167-
pub fn maybe_unstable_api_attr(unstable_api: bool) -> Option<syn::Attribute> {
169+
pub fn maybe_unstable_api_attr(unstable_api: bool) -> Option<proc_macro2::TokenStream> {
168170
if unstable_api {
169-
Some(unstable_api_attr())
171+
Some(unstable_api_attrs())
170172
} else {
171173
None
172174
}

guide/src/SUMMARY.md

+1
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
- [Function Overloads](./web-sys/function-overloads.md)
8989
- [Type Translations](./web-sys/type-translations.md)
9090
- [Inheritance](./web-sys/inheritance.md)
91+
- [Unstable APIs](./web-sys/unstable-apis.md)
9192

9293
- [Testing with `wasm-bindgen-test`](./wasm-bindgen-test/index.md)
9394
- [Usage](./wasm-bindgen-test/usage.md)

guide/src/web-sys/unstable-apis.md

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Unstable APIs
2+
3+
It's common for browsers to implement parts of a web API while the specification
4+
for that API is still being written. The API may require frequent changes as the
5+
specification continues to be developed, so the WebIDL is relatively unstable.
6+
7+
This causes some challenges for `web-sys` because it means `web-sys` would have
8+
to make breaking API changes whenever the WebIDL changes. It also means that
9+
previously published `web-sys` versions would be invalid, because the browser
10+
API may have been changed to match the updated WebIDL.
11+
12+
To avoid frequent breaking changes for unstable APIs, `web-sys` hides all
13+
unstable APIs through an attribute that looks like:
14+
15+
```rust
16+
#cfg[web_sys_unstable_apis]
17+
pub struct Foo;
18+
```
19+
20+
By hiding unstable APIs through an attribute, it's necessary for crates to
21+
explicitly opt-in to these reduced stability guarantees in order to use these
22+
APIs. Specifically, these APIs do not follow semver and may break whenever the
23+
WebIDL changes.
24+
25+
Crates can opt-in to unstable APIs at compile-time by passing the `cfg` flag
26+
`web_sys_unstable_apis`. Typically the `RUSTFLAGS` environment variable is used
27+
to do this. For example:
28+
29+
```bash
30+
RUSTFLAGS=--cfg=web_sys_unstable_apis cargo run
31+
```

0 commit comments

Comments
 (0)