pub struct ClientGuard { /* private fields */ }Expand description
A guard that automatically disconnects a ClaudeClient when dropped.
This provides RAII-style resource management for the client connection,
similar to Python’s async context manager (async with).
§Examples
use claude_agents_sdk::{ClaudeClient, ClientGuard};
use tokio_stream::StreamExt;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut client = ClaudeClient::new(None);
client.connect().await?;
// Create guard - client will be disconnected when guard is dropped
let mut guard = ClientGuard::new(client);
guard.client_mut().query("Hello!").await?;
let (response, _) = guard.client_mut().receive_response().await?;
println!("{}", response);
// Client automatically disconnected when guard goes out of scope
Ok(())
}Or using the convenience method:
use claude_agents_sdk::ClaudeClient;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut client = ClaudeClient::new(None);
client.connect().await?;
let guard = client.into_guard();
// Use guard.client() for all operations
// Automatically disconnects on drop
Ok(())
}Implementations§
Source§impl ClientGuard
impl ClientGuard
Sourcepub fn new(client: ClaudeClient) -> Self
pub fn new(client: ClaudeClient) -> Self
Create a new guard for the client.
§Note
If called outside of a Tokio runtime context, the guard will still work but automatic disconnect on drop will be skipped (with a warning logged).
Sourcepub fn client(&self) -> &ClaudeClient
pub fn client(&self) -> &ClaudeClient
Get a reference to the client.
Sourcepub fn client_mut(&mut self) -> &mut ClaudeClient
pub fn client_mut(&mut self) -> &mut ClaudeClient
Get a mutable reference to the client.
Sourcepub fn into_inner(self) -> ClaudeClient
pub fn into_inner(self) -> ClaudeClient
Take ownership of the client, preventing automatic disconnect.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for ClientGuard
impl !RefUnwindSafe for ClientGuard
impl Send for ClientGuard
impl Sync for ClientGuard
impl Unpin for ClientGuard
impl UnsafeUnpin for ClientGuard
impl !UnwindSafe for ClientGuard
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