Skip to content

Commit e4b31a3

Browse files
authored
Remove setters macro (#2882)
Since we decided on plain old rust objects long ago, we shouldn't have any setters. Any appropriate
1 parent 98fb02a commit e4b31a3

File tree

6 files changed

+66
-123
lines changed

6 files changed

+66
-123
lines changed

sdk/core/azure_core/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
### Breaking Changes
88

99
- Removed the `fs` module including the `FileStream` and `FileStreamBuilder` types. Moved to `examples/` for `typespec_client_core` to copy if needed.
10+
- Removed the `setters` macro.
1011

1112
### Bugs Fixed
1213

sdk/typespec/typespec_client_core/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
### Breaking Changes
88

99
- Removed the `fs` module including the `FileStream` and `FileStreamBuilder` types. Moved to `examples/` to copy if needed.
10+
- Removed the `setters` macro.
1011

1112
### Bugs Fixed
1213

sdk/typespec/typespec_client_core/examples/common/fs.rs

Lines changed: 46 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
33

4+
#![allow(dead_code)]
5+
46
use futures::{task::Poll, Future};
57
use std::{cmp::min, io::SeekFrom, pin::Pin, sync::Arc, task::Context};
68
use tokio::{
@@ -11,11 +13,10 @@ use tokio::{
1113
use tracing::debug;
1214
use typespec_client_core::{
1315
error::Result,
14-
http::{Body, RequestContent},
15-
setters,
1616
stream::{SeekableStream, DEFAULT_BUFFER_SIZE},
1717
};
1818

19+
/// Builds a [`FileStream`].
1920
#[derive(Debug)]
2021
pub struct FileStreamBuilder {
2122
handle: File,
@@ -37,15 +38,31 @@ impl FileStreamBuilder {
3738
}
3839
}
3940

40-
setters! {
41-
// #[doc = "Offset into the file to start reading from"]
42-
offset: u64 => Some(offset),
43-
// #[doc = "Amount of data to read from the file"]
44-
block_size: u64 => Some(block_size),
45-
// #[doc = "Amount of data to buffer in memory during streaming reads"]
46-
buffer_size: usize => Some(buffer_size),
41+
/// Offset into the file to start reading from
42+
pub fn offset(self, offset: u64) -> Self {
43+
Self {
44+
offset: Some(offset),
45+
..self
46+
}
47+
}
48+
49+
/// Amount of data to read from the file
50+
pub fn block_size(self, block_size: u64) -> Self {
51+
Self {
52+
block_size: Some(block_size),
53+
..self
54+
}
55+
}
56+
57+
/// Amount of data to buffer in memory during streaming reads
58+
pub fn buffer_size(self, buffer_size: usize) -> Self {
59+
Self {
60+
buffer_size: Some(buffer_size),
61+
..self
62+
}
4763
}
4864

65+
/// Build a [`FileStream`] from this `FileStreamBuilder`.
4966
pub async fn build(mut self) -> Result<FileStream> {
5067
let stream_size = self.handle.metadata().await?.len();
5168

@@ -159,29 +176,31 @@ impl futures::io::AsyncRead for FileStream {
159176
}
160177

161178
#[cfg(not(target_arch = "wasm32"))]
162-
impl From<&FileStream> for Body {
163-
fn from(stream: &FileStream) -> Self {
164-
Body::SeekableStream(Box::new(stream.clone()))
179+
mod convert {
180+
use super::FileStream;
181+
use typespec_client_core::http::{Body, RequestContent};
182+
183+
impl From<&FileStream> for Body {
184+
fn from(stream: &FileStream) -> Self {
185+
Body::SeekableStream(Box::new(stream.clone()))
186+
}
165187
}
166-
}
167188

168-
#[cfg(not(target_arch = "wasm32"))]
169-
impl From<FileStream> for Body {
170-
fn from(stream: FileStream) -> Self {
171-
Body::SeekableStream(Box::new(stream))
189+
impl From<FileStream> for Body {
190+
fn from(stream: FileStream) -> Self {
191+
Body::SeekableStream(Box::new(stream))
192+
}
172193
}
173-
}
174194

175-
#[cfg(not(target_arch = "wasm32"))]
176-
impl<T, F> From<&FileStream> for RequestContent<T, F> {
177-
fn from(stream: &FileStream) -> Self {
178-
Body::from(stream).into()
195+
impl<T, F> From<&FileStream> for RequestContent<T, F> {
196+
fn from(stream: &FileStream) -> Self {
197+
Body::from(stream).into()
198+
}
179199
}
180-
}
181200

182-
#[cfg(not(target_arch = "wasm32"))]
183-
impl<T, F> From<FileStream> for RequestContent<T, F> {
184-
fn from(stream: FileStream) -> Self {
185-
Body::from(stream).into()
201+
impl<T, F> From<FileStream> for RequestContent<T, F> {
202+
fn from(stream: FileStream) -> Self {
203+
Body::from(stream).into()
204+
}
186205
}
187206
}

sdk/typespec/typespec_client_core/src/http/options/retry.rs

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,11 @@ impl RetryOptions {
115115
/// # use typespec_client_core::time::Duration;
116116
/// # use typespec_client_core::http::{ExponentialRetryOptions, RetryOptions};
117117
/// RetryOptions::exponential(
118-
/// ExponentialRetryOptions::default()
119-
/// .max_retries(10u32)
120-
/// .initial_delay(Duration::seconds(1)),
118+
/// ExponentialRetryOptions {
119+
/// max_retries: 10u32,
120+
/// initial_delay: Duration::seconds(1),
121+
/// ..Default::default()
122+
/// }
121123
/// );
122124
/// ```
123125
#[derive(Clone, Debug)]
@@ -143,15 +145,6 @@ pub struct ExponentialRetryOptions {
143145
pub max_delay: Duration,
144146
}
145147

146-
impl ExponentialRetryOptions {
147-
setters! {
148-
initial_delay: Duration => initial_delay,
149-
max_retries: u32 => max_retries,
150-
max_total_elapsed: Duration => max_total_elapsed,
151-
max_delay: Duration => max_delay,
152-
}
153-
}
154-
155148
impl Default for ExponentialRetryOptions {
156149
fn default() -> Self {
157150
Self {
@@ -171,8 +164,10 @@ impl Default for ExponentialRetryOptions {
171164
/// ```
172165
/// # use typespec_client_core::http::{FixedRetryOptions, RetryOptions};
173166
/// RetryOptions::fixed(
174-
/// FixedRetryOptions::default()
175-
/// .max_retries(10u32)
167+
/// FixedRetryOptions {
168+
/// max_retries: 10u32,
169+
/// ..Default::default()
170+
/// }
176171
/// );
177172
/// ```
178173
#[derive(Clone, Debug)]
@@ -193,17 +188,6 @@ pub struct FixedRetryOptions {
193188
pub max_total_elapsed: Duration,
194189
}
195190

196-
impl FixedRetryOptions {
197-
setters! {
198-
#[doc = "Set the delay between retry attempts."]
199-
delay: Duration => delay,
200-
#[doc = "Set the maximum number of retry attempts before giving up."]
201-
max_retries: u32 => max_retries,
202-
#[doc = "Set the maximum permissible elapsed time since starting to retry."]
203-
max_total_elapsed: Duration => max_total_elapsed,
204-
}
205-
}
206-
207191
impl Default for FixedRetryOptions {
208192
fn default() -> Self {
209193
Self {

sdk/typespec/typespec_client_core/src/http/policies/retry/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -306,11 +306,11 @@ mod test {
306306
#[tokio::test]
307307
async fn test_retry_statuses() {
308308
let retries = 2u32;
309-
let retry_policy = RetryOptions::fixed(
310-
FixedRetryOptions::default()
311-
.delay(Duration::nanoseconds(1))
312-
.max_retries(retries),
313-
)
309+
let retry_policy = RetryOptions::fixed(FixedRetryOptions {
310+
delay: Duration::nanoseconds(1),
311+
max_retries: retries,
312+
..Default::default()
313+
})
314314
.to_policy();
315315
let ctx = Context::new();
316316
let url = Url::parse("http://localhost").unwrap();

sdk/typespec/typespec_client_core/src/macros.rs

Lines changed: 4 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -190,63 +190,6 @@ macro_rules! create_extensible_enum {
190190
);
191191
}
192192

193-
/// Creates setter methods
194-
///
195-
/// The methods created are of the form `$name` that takes an argument of type `$typ`
196-
/// and sets the field `$name` to result of calling `$transform` with the value of the argument.
197-
///
198-
/// In other words. The following macro call:
199-
/// ```
200-
/// # #[macro_use] extern crate typespec_client_core;
201-
/// struct MyStruct<'a> { foo: Option<&'a str> };
202-
/// impl <'a> MyStruct<'a> {
203-
/// setters! { foo: &'a str => Some(foo), }
204-
/// }
205-
/// ```
206-
/// Roughly expands to:
207-
/// ```
208-
/// struct MyStruct<'a> { foo: Option<&'a str> };
209-
/// impl <'a> MyStruct<'a> {
210-
/// fn foo(self, foo: &'a str) -> Self {
211-
/// Self {
212-
/// foo: Some(foo),
213-
/// ..self
214-
/// }
215-
/// }
216-
/// }
217-
/// ```
218-
#[macro_export]
219-
macro_rules! setters {
220-
(@single $(#[$meta:meta])* $name:ident : $typ:ty => $transform:expr) => {
221-
#[allow(clippy::redundant_field_names)]
222-
#[allow(clippy::needless_update)]
223-
#[allow(missing_docs)]
224-
#[must_use]
225-
$(#[$meta])*
226-
pub fn $name<P: ::std::convert::Into<$typ>>(self, $name: P) -> Self {
227-
let $name: $typ = $name.into();
228-
Self {
229-
$name: $transform,
230-
..self
231-
}
232-
}
233-
};
234-
// Terminal condition
235-
(@recurse) => {};
236-
// Recurse without transform
237-
(@recurse $(#[$meta:meta])* $name:ident : $typ:ty, $($tokens:tt)*) => {
238-
$crate::setters! { @recurse $(#[$meta])* $name: $typ => $name, $($tokens)* }
239-
};
240-
// Recurse with transform
241-
(@recurse $(#[$meta:meta])* $name:ident : $typ:ty => $transform:expr, $($tokens:tt)*) => {
242-
$crate::setters! { @single $(#[$meta])* $name : $typ => $transform }
243-
$crate::setters! { @recurse $($tokens)* }
244-
};
245-
($($tokens:tt)*) => {
246-
$crate::setters! { @recurse $($tokens)* }
247-
}
248-
}
249-
250193
#[cfg(test)]
251194
mod test {
252195
use serde::{Deserialize, Serialize};
@@ -277,14 +220,6 @@ mod test {
277220
b: u32,
278221
}
279222

280-
#[allow(dead_code)]
281-
impl Options {
282-
setters! {
283-
a: String => Some(a),
284-
b: u32 => b,
285-
}
286-
}
287-
288223
impl Default for Options {
289224
fn default() -> Self {
290225
Options { a: None, b: 1 }
@@ -310,7 +245,10 @@ mod test {
310245

311246
#[test]
312247
fn setters() {
313-
let options = Options::default().a("test".to_owned());
248+
let options = Options {
249+
a: Some("test".to_string()),
250+
..Default::default()
251+
};
314252

315253
assert_eq!(Some("test".to_owned()), options.a);
316254
assert_eq!(1, options.b);

0 commit comments

Comments
 (0)