pub struct SynchronizedHtml { /* private fields */ }Expand description
A parsed HTML document with the synchronization required for shared handler access.
Scraper 0.24’s atomic feature makes scraper::Html Send, but not Sync:
scraper::node::Element populates std::cell::OnceCell caches through shared references, and
atomic tendrils implement Send but not Sync. Consequently, Arc<scraper::Html> is not
Send and cannot be stored directly in a crawler context that moves between Tokio workers.
This type owns the required mutex rather than exposing a guard to handlers. Its query methods
require owned results, so the lock is always released before a handler can reach an .await
point. Adding an unsafe Sync implementation for direct shared access would be unsound (and
the workspace forbids unsafe code in any case).
Arc<scraper::Html> must not accidentally be treated as sendable shared state. This
compile-fail guard complements the positive assertions for SynchronizedHtml:
fn assert_send_sync<T: Send + Sync>() {}
assert_send_sync::<std::sync::Arc<scraper::Html>>();Implementations§
Source§impl SynchronizedHtml
impl SynchronizedHtml
Sourcepub fn with_html<R>(&self, query: impl FnOnce(&Html) -> R) -> R
pub fn with_html<R>(&self, query: impl FnOnce(&Html) -> R) -> R
Runs a synchronous query against the parsed document and returns its owned result.
The callback cannot return data borrowed from the document. Complete the query before an
.await, then move the returned value into subsequent asynchronous work.
§Panics
Panics if an earlier query callback panicked while holding the document lock.
Sourcepub fn lock(&self) -> MutexGuard<'_, Html>
pub fn lock(&self) -> MutexGuard<'_, Html>
Locks the document and exposes the complete scraper::Html API through dereferencing.
Prefer Self::with_html, Self::select, or Self::select_first when an owned result
is sufficient. A guard must be dropped before an .await point so another handler does not
block an executor thread waiting for the synchronous mutex.
§Panics
Panics if an earlier query panicked while holding the document lock.
Sourcepub fn select<T, F>(&self, selector: &Selector, map: F) -> Vec<T>where
F: for<'a> FnMut(ElementRef<'a>) -> T,
pub fn select<T, F>(&self, selector: &Selector, map: F) -> Vec<T>where
F: for<'a> FnMut(ElementRef<'a>) -> T,
Maps every element matching selector to an owned value.
The document lock is released before the returned vector is available to the caller.
Sourcepub fn select_first<T, F>(&self, selector: &Selector, map: F) -> Option<T>where
F: for<'a> FnOnce(ElementRef<'a>) -> T,
pub fn select_first<T, F>(&self, selector: &Selector, map: F) -> Option<T>where
F: for<'a> FnOnce(ElementRef<'a>) -> T,
Maps the first element matching selector to an owned value.
The document lock is released before the returned option is available to the caller.