pub struct BrowserHandle { /* private fields */ }Expand description
RAII handle for browser instances.
Automatically returns the browser to the pool when dropped. This ensures browsers are always returned even if the code panics.
§Thread Safety
BrowserHandle is Send but not Sync. This means:
- ✅ You can move it to another thread
- ❌ You cannot share it between threads simultaneously
This matches the typical usage pattern where a single request/task uses a browser exclusively.
§Usage
let browser_handle = pool.get()?;
// Use browser via Deref
let tab = browser_handle.new_tab()?;
// ... do work ...
// Browser automatically returned to pool when handle goes out of scope§Explicit Drop
If you need to return the browser early (before end of scope), you can explicitly drop the handle:
let browser = pool.get()?;
let tab = browser.new_tab()?;
// ... do work ...
// Return browser early
drop(browser);
// Browser is now back in the pool and available for others
// Attempting to use `browser` here would be a compile error§Panic Safety
The RAII pattern ensures browsers are returned even during panics:
let browser = pool.get()?;
// Even if this panics...
some_function_that_might_panic();
// ...the browser is still returned to the pool during unwindingImplementations§
Methods from Deref<Target = Browser>§
pub fn get_process_id(&self) -> Option<u32>
pub fn get_ws_url(&self) -> String
Sourcepub fn get_tabs(&self) -> &Arc<Mutex<Vec<Arc<Tab>>>>
pub fn get_tabs(&self) -> &Arc<Mutex<Vec<Arc<Tab>>>>
The tabs are behind an Arc and Mutex because they’re accessible from multiple threads
(including the one that handles incoming protocol events about new or changed tabs).
Sourcepub fn wait_for_initial_tab(&self) -> Result<Arc<Tab>, Error>
👎Deprecated since 1.0.4: Use new_tab() instead.
pub fn wait_for_initial_tab(&self) -> Result<Arc<Tab>, Error>
Chrome always launches with at least one tab. The reason we have to ‘wait’ is because information
about that tab isn’t available immediately after starting the process. Tabs are behind Arcs
because they each have their own thread which handles events and method responses directed to them.
Wait timeout: 10 secs
Sourcepub fn new_tab(&self) -> Result<Arc<Tab>, Error>
pub fn new_tab(&self) -> Result<Arc<Tab>, Error>
Create a new tab and return a handle to it.
If you want to specify its starting options, see new_tab_with_options.
let first_tab = browser.new_tab()?;
let new_tab = browser.new_tab()?;
let num_tabs = browser.get_tabs().lock().unwrap().len();
assert_eq!(2, num_tabs);Sourcepub fn new_tab_with_options(
&self,
create_target_params: CreateTarget,
) -> Result<Arc<Tab>, Error>
pub fn new_tab_with_options( &self, create_target_params: CreateTarget, ) -> Result<Arc<Tab>, Error>
Create a new tab with a starting url, height / width, context ID and ‘frame control’
let new_tab = browser.new_tab_with_options(CreateTarget {
url: "chrome://version",
width: Some(1024),
height: Some(800),
browser_context_id: None,
enable_begin_frame_control: None,
})?;Sourcepub fn new_context(&self) -> Result<Context<'_>, Error>
pub fn new_context(&self) -> Result<Context<'_>, Error>
Creates the equivalent of a new incognito window, AKA a browser context
Sourcepub fn register_missing_tabs(&self)
pub fn register_missing_tabs(&self)
Adds tabs that have not been opened with new_tab to the list of tabs
Sourcepub fn get_version(&self) -> Result<GetVersionReturnObject, Error>
pub fn get_version(&self) -> Result<GetVersionReturnObject, Error>
Get version information
let version_info = browser.get_version()?;
println!("User-Agent is `{}`", version_info.user_agent);Trait Implementations§
Source§impl Debug for BrowserHandle
impl Debug for BrowserHandle
Source§impl Deref for BrowserHandle
impl Deref for BrowserHandle
Source§fn deref(&self) -> &Self::Target
fn deref(&self) -> &Self::Target
Transparently access the underlying Browser.
This allows using all Browser methods directly on the handle:
let browser = pool.get()?;
// new_tab() is a Browser method, but works on BrowserHandle
let tab = browser.new_tab()?;§Panics
Panics if called after the browser has been returned to the pool. This should never happen in normal usage since the handle owns the browser until it’s dropped.
Source§impl Drop for BrowserHandle
impl Drop for BrowserHandle
Source§fn drop(&mut self)
fn drop(&mut self)
Automatically return browser to pool when handle is dropped.
This is the critical RAII pattern that ensures browsers are always returned to the pool, even if the code using them panics.
§Implementation Details
- Uses
Option::take()to move the browser out of the handle - Calls
BrowserPoolInner::return_browser()to return it - Safe to call multiple times (subsequent calls are no-ops)
Auto Trait Implementations§
impl Freeze for BrowserHandle
impl !RefUnwindSafe for BrowserHandle
impl Send for BrowserHandle
impl Sync for BrowserHandle
impl Unpin for BrowserHandle
impl !UnwindSafe for BrowserHandle
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoCollection<T> for T
impl<T> IntoCollection<T> for T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Paint for Twhere
T: ?Sized,
impl<T> Paint for Twhere
T: ?Sized,
Source§fn fg(&self, value: Color) -> Painted<&T>
fn fg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self with the foreground set to
value.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like red() and
green(), which have the same functionality but are
pithier.
§Example
Set foreground color to white using fg():
use yansi::{Paint, Color};
painted.fg(Color::White);Set foreground color to white using white().
use yansi::Paint;
painted.white();Source§fn bright_black(&self) -> Painted<&T>
fn bright_black(&self) -> Painted<&T>
Source§fn bright_red(&self) -> Painted<&T>
fn bright_red(&self) -> Painted<&T>
Source§fn bright_green(&self) -> Painted<&T>
fn bright_green(&self) -> Painted<&T>
Source§fn bright_yellow(&self) -> Painted<&T>
fn bright_yellow(&self) -> Painted<&T>
Source§fn bright_blue(&self) -> Painted<&T>
fn bright_blue(&self) -> Painted<&T>
Source§fn bright_magenta(&self) -> Painted<&T>
fn bright_magenta(&self) -> Painted<&T>
Source§fn bright_cyan(&self) -> Painted<&T>
fn bright_cyan(&self) -> Painted<&T>
Source§fn bright_white(&self) -> Painted<&T>
fn bright_white(&self) -> Painted<&T>
Source§fn bg(&self, value: Color) -> Painted<&T>
fn bg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self with the background set to
value.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like on_red() and
on_green(), which have the same functionality but
are pithier.
§Example
Set background color to red using fg():
use yansi::{Paint, Color};
painted.bg(Color::Red);Set background color to red using on_red().
use yansi::Paint;
painted.on_red();Source§fn on_primary(&self) -> Painted<&T>
fn on_primary(&self) -> Painted<&T>
Source§fn on_magenta(&self) -> Painted<&T>
fn on_magenta(&self) -> Painted<&T>
Source§fn on_bright_black(&self) -> Painted<&T>
fn on_bright_black(&self) -> Painted<&T>
Source§fn on_bright_red(&self) -> Painted<&T>
fn on_bright_red(&self) -> Painted<&T>
Source§fn on_bright_green(&self) -> Painted<&T>
fn on_bright_green(&self) -> Painted<&T>
Source§fn on_bright_yellow(&self) -> Painted<&T>
fn on_bright_yellow(&self) -> Painted<&T>
Source§fn on_bright_blue(&self) -> Painted<&T>
fn on_bright_blue(&self) -> Painted<&T>
Source§fn on_bright_magenta(&self) -> Painted<&T>
fn on_bright_magenta(&self) -> Painted<&T>
Source§fn on_bright_cyan(&self) -> Painted<&T>
fn on_bright_cyan(&self) -> Painted<&T>
Source§fn on_bright_white(&self) -> Painted<&T>
fn on_bright_white(&self) -> Painted<&T>
Source§fn attr(&self, value: Attribute) -> Painted<&T>
fn attr(&self, value: Attribute) -> Painted<&T>
Enables the styling Attribute value.
This method should be used rarely. Instead, prefer to use
attribute-specific builder methods like bold() and
underline(), which have the same functionality
but are pithier.
§Example
Make text bold using attr():
use yansi::{Paint, Attribute};
painted.attr(Attribute::Bold);Make text bold using using bold().
use yansi::Paint;
painted.bold();Source§fn rapid_blink(&self) -> Painted<&T>
fn rapid_blink(&self) -> Painted<&T>
Source§fn quirk(&self, value: Quirk) -> Painted<&T>
fn quirk(&self, value: Quirk) -> Painted<&T>
Enables the yansi Quirk value.
This method should be used rarely. Instead, prefer to use quirk-specific
builder methods like mask() and
wrap(), which have the same functionality but are
pithier.
§Example
Enable wrapping using .quirk():
use yansi::{Paint, Quirk};
painted.quirk(Quirk::Wrap);Enable wrapping using wrap().
use yansi::Paint;
painted.wrap();Source§fn clear(&self) -> Painted<&T>
👎Deprecated since 1.0.1: renamed to resetting() due to conflicts with Vec::clear().
The clear() method will be removed in a future release.
fn clear(&self) -> Painted<&T>
resetting() due to conflicts with Vec::clear().
The clear() method will be removed in a future release.Source§fn whenever(&self, value: Condition) -> Painted<&T>
fn whenever(&self, value: Condition) -> Painted<&T>
Conditionally enable styling based on whether the Condition value
applies. Replaces any previous condition.
See the crate level docs for more details.
§Example
Enable styling painted only when both stdout and stderr are TTYs:
use yansi::{Paint, Condition};
painted.red().on_yellow().whenever(Condition::STDOUTERR_ARE_TTY);