Skip to main content

Sections

Struct Sections 

Source
pub struct Sections { /* private fields */ }
Expand description

The sections a request reads, and how to read each one.

Built once, at startup, and handed to the framework adapter. Each entry is a closure the adapter calls when a request begins — never during it, which is what makes the reads agree.

Implementations§

Source§

impl Sections

Source

pub fn new() -> Self

An empty list.

Examples found in repository?
examples/core_snapshot.rs (line 63)
62fn main() {
63    let sections = Sections::new()
64        .section(|| Some(state().lock().expect("not poisoned").0.clone()))
65        .section(|| Some(state().lock().expect("not poisoned").1.clone()));
66
67    println!("sections: {:?}\n", sections.names());
68
69    // One `take()` is one reading of every section. Everything read
70    // *through the snapshot* afterwards is from that instant — a reload
71    // in between cannot tear it.
72    let snapshot = sections.take();
73
74    let server = snapshot.require::<Server>().expect("in scope");
75
76    reload(2); // ← a deployment lands mid-request
77
78    let features = snapshot.require::<Features>().expect("in scope");
79
80    println!("through one snapshot, across a reload:");
81    println!(
82        "  server   = port {}, generation {}",
83        server.port, server.generation
84    );
85    println!(
86        "  features = cache {}, generation {} (same instant)\n",
87        features.cache, features.generation
88    );
89    assert_eq!(server.generation, features.generation);
90
91    // The next request's snapshot sees the new state — this is
92    // per-request pinning, not staleness.
93    let next = sections.take();
94
95    println!("the next snapshot:");
96    println!(
97        "  features = {:?}\n",
98        next.require::<Features>().expect("in scope")
99    );
100
101    // And the error half: a type nobody declared answers by name.
102    let missing = snapshot.require::<String>().unwrap_err();
103
104    println!("asking for an undeclared section:\n  {missing}");
105}
Source

pub fn section<T, F>(self, read: F) -> Self
where T: Any + Send + Sync, F: Fn() -> Option<Arc<T>> + Send + Sync + 'static,

Adds one section, read by read.

let sections = Sections::new().section(try_current);

In a service read is || Database::try_current(), or move || handle.current() for a Dynamic<T>.

Registering the same type twice keeps the last reader, so a composed list can override one entry without rebuilding it.

Examples found in repository?
examples/core_snapshot.rs (line 64)
62fn main() {
63    let sections = Sections::new()
64        .section(|| Some(state().lock().expect("not poisoned").0.clone()))
65        .section(|| Some(state().lock().expect("not poisoned").1.clone()));
66
67    println!("sections: {:?}\n", sections.names());
68
69    // One `take()` is one reading of every section. Everything read
70    // *through the snapshot* afterwards is from that instant — a reload
71    // in between cannot tear it.
72    let snapshot = sections.take();
73
74    let server = snapshot.require::<Server>().expect("in scope");
75
76    reload(2); // ← a deployment lands mid-request
77
78    let features = snapshot.require::<Features>().expect("in scope");
79
80    println!("through one snapshot, across a reload:");
81    println!(
82        "  server   = port {}, generation {}",
83        server.port, server.generation
84    );
85    println!(
86        "  features = cache {}, generation {} (same instant)\n",
87        features.cache, features.generation
88    );
89    assert_eq!(server.generation, features.generation);
90
91    // The next request's snapshot sees the new state — this is
92    // per-request pinning, not staleness.
93    let next = sections.take();
94
95    println!("the next snapshot:");
96    println!(
97        "  features = {:?}\n",
98        next.require::<Features>().expect("in scope")
99    );
100
101    // And the error half: a type nobody declared answers by name.
102    let missing = snapshot.require::<String>().unwrap_err();
103
104    println!("asking for an undeclared section:\n  {missing}");
105}
Source

pub fn section_with_generation<T, F, G>(self, read: F, generation: G) -> Self
where T: Any + Send + Sync, F: Fn() -> Option<Arc<T>> + Send + Sync + 'static, G: Fn() -> u64 + Send + Sync + 'static,

Adds one section, with the install counter that says when it moved.

What sections! uses. The counter lets take tell a snapshot that straddled a reload from one that did not; a section registered through section has none, and a list containing one cannot make that check.

Source

pub fn is_consistent(&self) -> bool

Whether every section can say when it last moved.

false when any was registered through section, which takes a reader and nothing else — take then reads once without checking.

Source

pub fn take(&self) -> Snapshot

Reads every section once.

Called by the adapter when a request begins. A section that has not loaded is left out rather than failing the request: the handler that asks for it gets NotInScope::NotLoaded, and a handler that does not ask is unaffected.

Examples found in repository?
examples/core_snapshot.rs (line 72)
62fn main() {
63    let sections = Sections::new()
64        .section(|| Some(state().lock().expect("not poisoned").0.clone()))
65        .section(|| Some(state().lock().expect("not poisoned").1.clone()));
66
67    println!("sections: {:?}\n", sections.names());
68
69    // One `take()` is one reading of every section. Everything read
70    // *through the snapshot* afterwards is from that instant — a reload
71    // in between cannot tear it.
72    let snapshot = sections.take();
73
74    let server = snapshot.require::<Server>().expect("in scope");
75
76    reload(2); // ← a deployment lands mid-request
77
78    let features = snapshot.require::<Features>().expect("in scope");
79
80    println!("through one snapshot, across a reload:");
81    println!(
82        "  server   = port {}, generation {}",
83        server.port, server.generation
84    );
85    println!(
86        "  features = cache {}, generation {} (same instant)\n",
87        features.cache, features.generation
88    );
89    assert_eq!(server.generation, features.generation);
90
91    // The next request's snapshot sees the new state — this is
92    // per-request pinning, not staleness.
93    let next = sections.take();
94
95    println!("the next snapshot:");
96    println!(
97        "  features = {:?}\n",
98        next.require::<Features>().expect("in scope")
99    );
100
101    // And the error half: a type nobody declared answers by name.
102    let missing = snapshot.require::<String>().unwrap_err();
103
104    println!("asking for an undeclared section:\n  {missing}");
105}
Source

pub fn len(&self) -> usize

How many sections are registered.

Source

pub fn is_empty(&self) -> bool

Whether nothing is registered.

Source

pub fn names(&self) -> Vec<&'static str>

The registered type names, in order.

Examples found in repository?
examples/core_snapshot.rs (line 67)
62fn main() {
63    let sections = Sections::new()
64        .section(|| Some(state().lock().expect("not poisoned").0.clone()))
65        .section(|| Some(state().lock().expect("not poisoned").1.clone()));
66
67    println!("sections: {:?}\n", sections.names());
68
69    // One `take()` is one reading of every section. Everything read
70    // *through the snapshot* afterwards is from that instant — a reload
71    // in between cannot tear it.
72    let snapshot = sections.take();
73
74    let server = snapshot.require::<Server>().expect("in scope");
75
76    reload(2); // ← a deployment lands mid-request
77
78    let features = snapshot.require::<Features>().expect("in scope");
79
80    println!("through one snapshot, across a reload:");
81    println!(
82        "  server   = port {}, generation {}",
83        server.port, server.generation
84    );
85    println!(
86        "  features = cache {}, generation {} (same instant)\n",
87        features.cache, features.generation
88    );
89    assert_eq!(server.generation, features.generation);
90
91    // The next request's snapshot sees the new state — this is
92    // per-request pinning, not staleness.
93    let next = sections.take();
94
95    println!("the next snapshot:");
96    println!(
97        "  features = {:?}\n",
98        next.require::<Features>().expect("in scope")
99    );
100
101    // And the error half: a type nobody declared answers by name.
102    let missing = snapshot.require::<String>().unwrap_err();
103
104    println!("asking for an undeclared section:\n  {missing}");
105}

Trait Implementations§

Source§

impl Debug for Sections

Source§

fn fmt(&self, formatter: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Sections

Source§

fn default() -> Sections

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.