Skip to main content

SynchronizedHtml

Struct SynchronizedHtml 

Source
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

Source

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.

Source

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.

Source

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.

Source

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.

Trait Implementations§

Source§

impl Debug for SynchronizedHtml

Source§

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

Formats the value using the given formatter. 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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more