pub struct Channel { /* private fields */ }Expand description
Channel provides RPC communication for a ChannelOwner.
Every ChannelOwner has a Channel that sends method calls to the Playwright server and receives responses.
§Architecture
In the JavaScript implementation, Channel is a Proxy object that intercepts method calls and forwards them to the connection.
In Python, Channel is a class with explicit method forwarding.
In Rust, we provide an explicit send method that handles:
- Serialization of parameters
- Sending to connection with object’s GUID
- Waiting for response
- Deserialization of result
§Example
ⓘ
// Example of using Channel to send RPC calls
use playwright_core::channel::Channel;
use serde::{Serialize, Deserialize};
#[derive(Serialize)]
struct LaunchParams {
headless: bool,
}
#[derive(Deserialize)]
struct LaunchResult {
browser: BrowserRef,
}
// Protocol response references use Arc<str> for performance
#[derive(Deserialize)]
struct BrowserRef {
guid: String, // Simplified for example; actual implementation uses Arc<str>
}
async fn example(channel: &Channel) -> Result<(), Box<dyn std::error::Error>> {
let params = LaunchParams { headless: true };
let result: LaunchResult = channel.send("launch", params).await?;
println!("Browser GUID: {}", result.browser.guid);
Ok(())
}Implementations§
Source§impl Channel
impl Channel
Sourcepub fn new(guid: Arc<str>, connection: Arc<dyn ConnectionLike>) -> Self
pub fn new(guid: Arc<str>, connection: Arc<dyn ConnectionLike>) -> Self
Creates a new Channel for the given object GUID.
§Arguments
guid- The GUID of the ChannelOwner this channel representsconnection- The connection to send messages through
Sourcepub async fn send<P: Serialize, R: DeserializeOwned>(
&self,
method: &str,
params: P,
) -> Result<R>
pub async fn send<P: Serialize, R: DeserializeOwned>( &self, method: &str, params: P, ) -> Result<R>
Sends a method call to the Playwright server and awaits the response.
This method:
- Serializes
paramsto JSON - Sends a JSON-RPC request to the server via the connection
- Waits for the response (correlated by request ID)
- Deserializes the response to type
R - Returns the result or an error
§Type Parameters
P- Parameter type (must be serializable)R- Result type (must be deserializable)
§Arguments
method- The method name to call (e.g., “launch”, “goto”)params- The parameters to send
§Example
ⓘ
#[derive(Serialize)]
struct GotoParams<'a> {
url: &'a str,
}
#[derive(Deserialize)]
struct GotoResult {}
let params = GotoParams { url: "https://example.com" };
let _result: GotoResult = channel.send("goto", params).await?;Sourcepub async fn send_no_params<R: DeserializeOwned>(
&self,
method: &str,
) -> Result<R>
pub async fn send_no_params<R: DeserializeOwned>( &self, method: &str, ) -> Result<R>
Sends a method call with no parameters.
Convenience method for calls that don’t need parameters.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Channel
impl !RefUnwindSafe for Channel
impl Send for Channel
impl Sync for Channel
impl Unpin for Channel
impl !UnwindSafe for Channel
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
Mutably borrows from an owned value. Read more