1- #[ cfg( feature = "runtime" ) ]
2- use crate :: cancel_query;
31use crate :: codec:: BackendMessages ;
42use crate :: config:: { Host , SslMode } ;
53use crate :: connection:: { Request , RequestMessages } ;
@@ -14,7 +12,7 @@ use crate::to_statement::ToStatement;
1412use crate :: types:: { Oid , ToSql , Type } ;
1513#[ cfg( feature = "runtime" ) ]
1614use crate :: Socket ;
17- use crate :: { cancel_query_raw , copy_in, copy_out, query, CopyInSink , Transaction } ;
15+ use crate :: { copy_in, copy_out, query, CancelToken , CopyInSink , Transaction } ;
1816use crate :: { prepare, SimpleQueryMessage } ;
1917use crate :: { simple_query, Row } ;
2018use crate :: { Error , Statement } ;
@@ -451,42 +449,43 @@ impl Client {
451449 Ok ( Transaction :: new ( self ) )
452450 }
453451
452+ /// Constructs a cancellation token that can later be used to request
453+ /// cancellation of a query running on the connection associated with
454+ /// this client.
455+ pub fn cancel_token ( & self ) -> CancelToken {
456+ CancelToken {
457+ #[ cfg( feature = "runtime" ) ]
458+ socket_config : self . socket_config . clone ( ) ,
459+ ssl_mode : self . ssl_mode ,
460+ process_id : self . process_id ,
461+ secret_key : self . secret_key ,
462+ }
463+ }
464+
454465 /// Attempts to cancel an in-progress query.
455466 ///
456467 /// The server provides no information about whether a cancellation attempt was successful or not. An error will
457468 /// only be returned if the client was unable to connect to the database.
458469 ///
459470 /// Requires the `runtime` Cargo feature (enabled by default).
460471 #[ cfg( feature = "runtime" ) ]
472+ #[ deprecated( since = "0.6.0" , note = "use Client::cancel_token() instead" ) ]
461473 pub async fn cancel_query < T > ( & self , tls : T ) -> Result < ( ) , Error >
462474 where
463475 T : MakeTlsConnect < Socket > ,
464476 {
465- cancel_query:: cancel_query (
466- self . socket_config . clone ( ) ,
467- self . ssl_mode ,
468- tls,
469- self . process_id ,
470- self . secret_key ,
471- )
472- . await
477+ self . cancel_token ( ) . cancel_query ( tls) . await
473478 }
474479
475480 /// Like `cancel_query`, but uses a stream which is already connected to the server rather than opening a new
476481 /// connection itself.
482+ #[ deprecated( since = "0.6.0" , note = "use Client::cancel_token() instead" ) ]
477483 pub async fn cancel_query_raw < S , T > ( & self , stream : S , tls : T ) -> Result < ( ) , Error >
478484 where
479485 S : AsyncRead + AsyncWrite + Unpin ,
480486 T : TlsConnect < S > ,
481487 {
482- cancel_query_raw:: cancel_query_raw (
483- stream,
484- self . ssl_mode ,
485- tls,
486- self . process_id ,
487- self . secret_key ,
488- )
489- . await
488+ self . cancel_token ( ) . cancel_query_raw ( stream, tls) . await
490489 }
491490
492491 /// Determines if the connection to the server has already closed.
0 commit comments