From 160ddb1aae32ae4d69c5ca202dfb09bc242caff1 Mon Sep 17 00:00:00 2001 From: jethrogb Date: Mon, 29 Jun 2020 16:33:57 +0200 Subject: [PATCH 1/4] Clarify defining static items in a generic scope --- src/items/static-items.md | 48 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/src/items/static-items.md b/src/items/static-items.md index 284e78e28..9bf566155 100644 --- a/src/items/static-items.md +++ b/src/items/static-items.md @@ -26,6 +26,54 @@ statics: The initializer expression must be omitted in an [external block], and must be provided for free static items. +## Statics & generics + +A static item defined in a generic scope (for example in a blanket or default +implementation) will result in exactly one static item being defined, as if +the static definition was pulled out of the current scope into the module. +There will *not* be one item per monomorphization. + +This code: + +```rust +use std::sync::atomic::{AtomicUsize, Ordering}; + +trait Tr { + fn default_impl() { + static COUNTER: AtomicUsize = AtomicUsize::new(0); + println!("counter was {}", COUNTER.fetch_add(1, Ordering::Relaxed)); + } + + fn blanket_impl(); +} + +struct Ty1 {} +struct Ty2 {} + +impl Tr for T { + fn blanket_impl() { + static COUNTER: AtomicUsize = AtomicUsize::new(0); + println!("counter was {}", COUNTER.fetch_add(1, Ordering::Relaxed)); + } +} + +fn main() { + ::default_impl(); + ::default_impl(); + ::blanket_impl(); + ::blanket_impl(); +} +``` + +prints + +```text +counter was 0 +counter was 1 +counter was 0 +counter was 1 +``` + ## Mutable statics If a static item is declared with the `mut` keyword, then it is allowed to be From d759df1d0af83021fc1a04057f75909770ed6509 Mon Sep 17 00:00:00 2001 From: Josh Triplett Date: Tue, 29 Jun 2021 21:04:17 -0700 Subject: [PATCH 2/4] Apply suggestions from code review Label the messages distinctly. Co-authored-by: Yuki Okushi --- src/items/static-items.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/items/static-items.md b/src/items/static-items.md index 9bf566155..1982cddb0 100644 --- a/src/items/static-items.md +++ b/src/items/static-items.md @@ -41,7 +41,7 @@ use std::sync::atomic::{AtomicUsize, Ordering}; trait Tr { fn default_impl() { static COUNTER: AtomicUsize = AtomicUsize::new(0); - println!("counter was {}", COUNTER.fetch_add(1, Ordering::Relaxed)); + println!("default_impl: counter was {}", COUNTER.fetch_add(1, Ordering::Relaxed)); } fn blanket_impl(); @@ -53,7 +53,7 @@ struct Ty2 {} impl Tr for T { fn blanket_impl() { static COUNTER: AtomicUsize = AtomicUsize::new(0); - println!("counter was {}", COUNTER.fetch_add(1, Ordering::Relaxed)); + println!("blanket_impl: counter was {}", COUNTER.fetch_add(1, Ordering::Relaxed)); } } From c3f43d7fcbee94c6278b55236e31f988dc59870b Mon Sep 17 00:00:00 2001 From: Yuki Okushi Date: Thu, 1 Jul 2021 07:43:03 +0900 Subject: [PATCH 3/4] Update the output --- src/items/static-items.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/items/static-items.md b/src/items/static-items.md index 1982cddb0..f4de6f230 100644 --- a/src/items/static-items.md +++ b/src/items/static-items.md @@ -68,10 +68,10 @@ fn main() { prints ```text -counter was 0 -counter was 1 -counter was 0 -counter was 1 +default_impl: counter was 0 +default_impl: counter was 1 +blanket_impl: counter was 0 +blanket_impl: counter was 1 ``` ## Mutable statics From bbb69d1e29ff3dc3acaa3f0011bf9b1c9991322c Mon Sep 17 00:00:00 2001 From: Yuki Okushi Date: Thu, 1 Jul 2021 07:45:35 +0900 Subject: [PATCH 4/4] Fix style issue --- src/items/static-items.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/items/static-items.md b/src/items/static-items.md index f4de6f230..e543b4fa6 100644 --- a/src/items/static-items.md +++ b/src/items/static-items.md @@ -30,7 +30,7 @@ provided for free static items. A static item defined in a generic scope (for example in a blanket or default implementation) will result in exactly one static item being defined, as if -the static definition was pulled out of the current scope into the module. +the static definition was pulled out of the current scope into the module. There will *not* be one item per monomorphization. This code: