Skip to content

Commit d2441c3

Browse files
committed
Add example code for invalidate_current_thread_spans
1 parent 43011bf commit d2441c3

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,11 @@ features = ["span-locations"]
2424
unicode-ident = "1.0"
2525

2626
[dev-dependencies]
27+
flate2 = "1.0"
2728
quote = { version = "1.0", default_features = false }
29+
rayon = "1.0"
2830
rustversion = "1"
31+
tar = "0.4"
2932

3033
[features]
3134
proc-macro = []

src/extra.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,50 @@ use core::fmt::{self, Debug};
2020
/// gigabytes). After a wraparound, `Span` methods such as `source_text()` can
2121
/// return wrong data.
2222
///
23+
/// # Example
24+
///
25+
/// As of late 2023, there is 200 GB of Rust code published on crates.io.
26+
/// Looking at just the newest version of every crate, it is 16 GB of code. So a
27+
/// workload that involves parsing it all would overflow a 32-bit source
28+
/// location unless spans are being invalidated.
29+
///
30+
/// ```
31+
/// use flate2::read::GzDecoder;
32+
/// use std::ffi::OsStr;
33+
/// use std::io::{BufReader, Read};
34+
/// use std::str::FromStr;
35+
/// use tar::Archive;
36+
///
37+
/// rayon::scope(|s| {
38+
/// for krate in every_version_of_every_crate() {
39+
/// s.spawn(move |_| {
40+
/// proc_macro2::extra::invalidate_current_thread_spans();
41+
///
42+
/// let reader = BufReader::new(krate);
43+
/// let tar = GzDecoder::new(reader);
44+
/// let mut archive = Archive::new(tar);
45+
/// for entry in archive.entries().unwrap() {
46+
/// let mut entry = entry.unwrap();
47+
/// let path = entry.path().unwrap();
48+
/// if path.extension() != Some(OsStr::new("rs")) {
49+
/// continue;
50+
/// }
51+
/// let mut content = String::new();
52+
/// entry.read_to_string(&mut content).unwrap();
53+
/// match proc_macro2::TokenStream::from_str(&content) {
54+
/// Ok(tokens) => {/* ... */},
55+
/// Err(_) => continue,
56+
/// }
57+
/// }
58+
/// });
59+
/// }
60+
/// });
61+
/// #
62+
/// # fn every_version_of_every_crate() -> Vec<std::fs::File> {
63+
/// # Vec::new()
64+
/// # }
65+
/// ```
66+
///
2367
/// # Panics
2468
///
2569
/// This function is not applicable to and will panic if called from a

0 commit comments

Comments
 (0)