Skip to content

Commit 013ed41

Browse files
authored
Add Context::to_borrowed (#2895)
Resolves #2591, though we need to eventually remove `with_context` so I've added `expect` to crates until we get an emitter fix. As is, this can target `main` for now.
1 parent 2e2c53c commit 013ed41

File tree

7 files changed

+32
-13
lines changed
  • sdk
    • core/azure_core_test/src/proxy
    • cosmos/azure_data_cosmos/src/query
    • keyvault
      • azure_security_keyvault_certificates/src
      • azure_security_keyvault_keys/src
      • azure_security_keyvault_secrets/src
    • storage/azure_storage_blob/src
    • typespec/typespec_client_core/src/http

7 files changed

+32
-13
lines changed

sdk/core/azure_core_test/src/proxy/client.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use azure_core::{
1414
http::{
1515
headers::{AsHeaders, ACCEPT, CONTENT_TYPE},
1616
request::{Request, RequestContent},
17-
ClientMethodOptions, ClientOptions, Context, Method, Pipeline, Response, Url,
17+
ClientMethodOptions, ClientOptions, Method, Pipeline, Response, Url,
1818
},
1919
Bytes, Result,
2020
};
@@ -56,7 +56,7 @@ impl Client {
5656
options: Option<ClientRecordStartOptions<'_>>,
5757
) -> Result<RecordStartResult> {
5858
let options = options.unwrap_or_default();
59-
let ctx = Context::with_context(&options.method_options.context);
59+
let ctx = options.method_options.context.to_borrowed();
6060
let mut url = self.endpoint.clone();
6161
url = url.join("/Record/Start")?;
6262
let mut request = Request::new(url, Method::Post);
@@ -77,7 +77,7 @@ impl Client {
7777
options: Option<ClientRecordStopOptions<'_>>,
7878
) -> Result<()> {
7979
let options = options.unwrap_or_default();
80-
let ctx = Context::with_context(&options.method_options.context);
80+
let ctx = options.method_options.context.to_borrowed();
8181
let mut url = self.endpoint.clone();
8282
url = url.join("/Record/Stop")?;
8383
let mut request = Request::new(url, Method::Post);
@@ -100,7 +100,7 @@ impl Client {
100100
stringify!(recording_id),
101101
options.recording_id.map(AsRef::as_ref),
102102
);
103-
let ctx = Context::with_context(&options.method_options.context);
103+
let ctx = options.method_options.context.to_borrowed();
104104
let mut url = self.endpoint.clone();
105105
url = url.join("/Playback/Start")?;
106106
let mut request = Request::new(url, Method::Post);
@@ -123,7 +123,7 @@ impl Client {
123123
options: Option<ClientPlaybackStopOptions<'_>>,
124124
) -> Result<()> {
125125
let options = options.unwrap_or_default();
126-
let ctx = Context::with_context(&options.method_options.context);
126+
let ctx = options.method_options.context.to_borrowed();
127127
let mut url = self.endpoint.clone();
128128
url = url.join("/Playback/Stop")?;
129129
let mut request = Request::new(url, Method::Post);
@@ -145,7 +145,7 @@ impl Client {
145145
stringify!(recording_id),
146146
options.recording_id.map(AsRef::as_ref),
147147
);
148-
let ctx = Context::with_context(&options.method_options.context);
148+
let ctx = options.method_options.context.to_borrowed();
149149
let mut url = self.endpoint.clone();
150150
url = url.join("/Admin/SetMatcher")?;
151151
let mut request = Request::new(url, Method::Post);
@@ -174,7 +174,7 @@ impl Client {
174174
stringify!(recording_id),
175175
options.recording_id.map(AsRef::as_ref),
176176
);
177-
let ctx = Context::with_context(&options.method_options.context);
177+
let ctx = options.method_options.context.to_borrowed();
178178
let mut url = self.endpoint.clone();
179179
url = url.join("/Admin/AddSanitizer")?;
180180
let mut request = Request::new(url, Method::Post);
@@ -197,7 +197,7 @@ impl Client {
197197
stringify!(recording_id),
198198
options.recording_id.map(AsRef::as_ref),
199199
);
200-
let ctx = Context::with_context(&options.method_options.context);
200+
let ctx = options.method_options.context.to_borrowed();
201201
let mut url = self.endpoint.clone();
202202
url = url.join("/Admin/RemoveSanitizers")?;
203203
let mut request = Request::new(url, Method::Post);
@@ -220,7 +220,7 @@ impl Client {
220220
stringify!(recording_id),
221221
options.recording_id.map(AsRef::as_ref),
222222
);
223-
let ctx = Context::with_context(&options.method_options.context);
223+
let ctx = options.method_options.context.to_borrowed();
224224
let mut url = self.endpoint.clone();
225225
url = url.join("/Admin/Reset")?;
226226
let mut request = Request::new(url, Method::Post);

sdk/cosmos/azure_data_cosmos/src/query/executor.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ impl<T: DeserializeOwned + Send + 'static> QueryExecutor<T> {
7979
let query_plan = get_query_plan(
8080
&self.http_pipeline,
8181
&self.items_link,
82-
Context::with_context(&self.context),
82+
self.context.to_borrowed(),
8383
&self.query,
8484
self.query_engine.supported_features()?,
8585
)
@@ -90,7 +90,7 @@ impl<T: DeserializeOwned + Send + 'static> QueryExecutor<T> {
9090
let pkranges = get_pkranges(
9191
&self.http_pipeline,
9292
&self.container_link,
93-
Context::with_context(&self.context),
93+
self.context.to_borrowed(),
9494
)
9595
.await?
9696
.into_body()
@@ -145,7 +145,7 @@ impl<T: DeserializeOwned + Send + 'static> QueryExecutor<T> {
145145
let resp = self
146146
.http_pipeline
147147
.send_raw(
148-
Context::with_context(&self.context),
148+
self.context.to_borrowed(),
149149
&mut query_request,
150150
self.items_link.clone(),
151151
)

sdk/keyvault/azure_security_keyvault_certificates/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
pub mod clients;
77
#[allow(unused_imports)]
8+
#[expect(deprecated, reason = "requires emitter update")]
89
mod generated;
910
pub mod models;
1011
mod resource;

sdk/keyvault/azure_security_keyvault_keys/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#![doc = include_str!("../README.md")]
55

6+
#[expect(deprecated, reason = "requires emitter update")]
67
mod generated;
78
mod resource;
89

sdk/keyvault/azure_security_keyvault_secrets/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#![doc = include_str!("../README.md")]
55

6+
#[expect(deprecated, reason = "requires emitter update")]
67
mod generated;
78
mod resource;
89

sdk/storage/azure_storage_blob/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#![allow(unused_imports)]
99

1010
pub mod clients;
11+
#[expect(deprecated, reason = "requires emitter update")]
1112
mod generated;
1213
mod parsers;
1314
mod pipeline;

sdk/typespec/typespec_client_core/src/http/context.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ impl<'a> Context<'a> {
2626
/// Returns a new `Context` that borrows the type map of the given `context`.
2727
///
2828
/// Once you [`Context::insert`] entities the type map is copied.
29+
#[deprecated(since = "0.7.0", note = "use to_borrowed() instead")]
2930
#[must_use]
3031
pub fn with_context<'b>(context: &'a Context) -> Context<'b>
3132
where
@@ -121,6 +122,20 @@ impl<'a> Context<'a> {
121122
type_map: Cow::Owned(type_map),
122123
}
123124
}
125+
126+
/// Returns a new `Context` that borrows the type map of the given `context`.
127+
///
128+
/// Once you [`Context::insert`] entities the type map is copied.
129+
#[must_use]
130+
pub fn to_borrowed<'b>(&'a self) -> Context<'b>
131+
where
132+
'a: 'b,
133+
{
134+
let type_map = self.type_map.as_ref();
135+
Context {
136+
type_map: Cow::Borrowed(type_map),
137+
}
138+
}
124139
}
125140

126141
impl Default for Context<'_> {
@@ -223,7 +238,7 @@ mod tests {
223238
#[test]
224239
fn with_context_borrows() {
225240
let a = Context::new().with_value("a".to_string());
226-
let mut b = Context::with_context(&a);
241+
let mut b = a.to_borrowed();
227242

228243
// TODO: Use is_owned(), is_borrowed() once stabilized.
229244
let a_ptr = std::ptr::addr_of!(*a.type_map);

0 commit comments

Comments
 (0)