Skip to content

Commit 18cfdb3

Browse files
committed
RUST-788 Use impl AsRef<str> instead of &str (C-GENERIC)
1 parent 9cb7dc6 commit 18cfdb3

File tree

7 files changed

+35
-30
lines changed

7 files changed

+35
-30
lines changed

src/client/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ impl Client {
9898
///
9999
/// See the documentation on
100100
/// [`ClientOptions::parse`](options/struct.ClientOptions.html#method.parse) for more details.
101-
pub async fn with_uri_str(uri: &str) -> Result<Self> {
102-
let options = ClientOptions::parse_uri(uri, None).await?;
101+
pub async fn with_uri_str(uri: impl AsRef<str>) -> Result<Self> {
102+
let options = ClientOptions::parse_uri(uri.as_ref(), None).await?;
103103

104104
Client::with_options(options)
105105
}

src/client/options/mod.rs

+8-7
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,8 @@ impl Hash for StreamAddress {
148148

149149
impl StreamAddress {
150150
/// Parses an address string into a `StreamAddress`.
151-
pub fn parse(address: &str) -> Result<Self> {
151+
pub fn parse(address: impl AsRef<str>) -> Result<Self> {
152+
let address = address.as_ref();
152153
let mut parts = address.split(':');
153154

154155
let hostname = match parts.next() {
@@ -776,16 +777,16 @@ impl ClientOptions {
776777
/// Note: if the `sync` feature is enabled, then this method will be replaced with [the sync
777778
/// version](#method.parse-1).
778779
#[cfg(not(feature = "sync"))]
779-
pub async fn parse(s: &str) -> Result<Self> {
780+
pub async fn parse(s: impl AsRef<str>) -> Result<Self> {
780781
Self::parse_uri(s, None).await
781782
}
782783

783784
/// This method will be present if the `sync` feature is enabled. It's otherwise identical to
784785
/// [the async version](#method.parse)
785786
#[cfg(any(feature = "sync", docsrs))]
786787
#[cfg_attr(docsrs, doc(cfg(feature = "sync")))]
787-
pub fn parse(s: &str) -> Result<Self> {
788-
crate::RUNTIME.block_on(Self::parse_uri(s, None))
788+
pub fn parse(s: impl AsRef<str>) -> Result<Self> {
789+
crate::RUNTIME.block_on(Self::parse_uri(s.as_ref(), None))
789790
}
790791

791792
/// Parses a MongoDB connection string into a `ClientOptions` struct.
@@ -804,7 +805,7 @@ impl ClientOptions {
804805
/// version](#method.parse_with_resolver_config-1).
805806
#[cfg(not(feature = "sync"))]
806807
pub async fn parse_with_resolver_config(
807-
uri: &str,
808+
uri: impl AsRef<str>,
808809
resolver_config: ResolverConfig,
809810
) -> Result<Self> {
810811
Self::parse_uri(uri, Some(resolver_config)).await
@@ -821,10 +822,10 @@ impl ClientOptions {
821822
/// Populate this `ClientOptions` from the given URI, optionally using the resolver config for
822823
/// DNS lookups.
823824
pub(crate) async fn parse_uri(
824-
uri: &str,
825+
uri: impl AsRef<str>,
825826
resolver_config: Option<ResolverConfig>,
826827
) -> Result<Self> {
827-
let parser = ClientOptionsParser::parse(uri)?;
828+
let parser = ClientOptionsParser::parse(uri.as_ref())?;
828829
let srv = parser.srv;
829830
let auth_source_present = parser.auth_source.is_some();
830831
let mut options: Self = parser.into();

src/coll/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ where
384384

385385
async fn distinct_common(
386386
&self,
387-
field_name: &str,
387+
field_name: impl AsRef<str>,
388388
filter: impl Into<Option<Document>>,
389389
options: impl Into<Option<DistinctOptions>>,
390390
session: impl Into<Option<&mut ClientSession>>,
@@ -394,7 +394,7 @@ where
394394

395395
let op = Distinct::new(
396396
self.namespace(),
397-
field_name.to_string(),
397+
field_name.as_ref().to_string(),
398398
filter.into(),
399399
options,
400400
);
@@ -404,7 +404,7 @@ where
404404
/// Finds the distinct values of the field specified by `field_name` across the collection.
405405
pub async fn distinct(
406406
&self,
407-
field_name: &str,
407+
field_name: impl AsRef<str>,
408408
filter: impl Into<Option<Document>>,
409409
options: impl Into<Option<DistinctOptions>>,
410410
) -> Result<Vec<Bson>> {
@@ -416,7 +416,7 @@ where
416416
/// the provided `ClientSession`.
417417
pub async fn distinct_with_session(
418418
&self,
419-
field_name: &str,
419+
field_name: impl AsRef<str>,
420420
filter: impl Into<Option<Document>>,
421421
options: impl Into<Option<DistinctOptions>>,
422422
session: &mut ClientSession,

src/db/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ impl Database {
284284

285285
async fn create_collection_common(
286286
&self,
287-
name: &str,
287+
name: impl AsRef<str>,
288288
options: impl Into<Option<CreateCollectionOptions>>,
289289
session: impl Into<Option<&mut ClientSession>>,
290290
) -> Result<()> {
@@ -294,7 +294,7 @@ impl Database {
294294
let create = Create::new(
295295
Namespace {
296296
db: self.name().to_string(),
297-
coll: name.to_string(),
297+
coll: name.as_ref().to_string(),
298298
},
299299
options,
300300
);
@@ -307,7 +307,7 @@ impl Database {
307307
/// not needed if no special options are required.
308308
pub async fn create_collection(
309309
&self,
310-
name: &str,
310+
name: impl AsRef<str>,
311311
options: impl Into<Option<CreateCollectionOptions>>,
312312
) -> Result<()> {
313313
self.create_collection_common(name, options, None).await
@@ -320,7 +320,7 @@ impl Database {
320320
/// not needed if no special options are required.
321321
pub async fn create_collection_with_session(
322322
&self,
323-
name: &str,
323+
name: impl AsRef<str>,
324324
options: impl Into<Option<CreateCollectionOptions>>,
325325
session: &mut ClientSession,
326326
) -> Result<()> {

src/sync/client.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ impl Client {
6666
/// See the documentation on
6767
/// [`ClientOptions::parse`](../options/struct.ClientOptions.html#method.parse) for more
6868
/// details.
69-
pub fn with_uri_str(uri: &str) -> Result<Self> {
70-
let async_client = RUNTIME.block_on(AsyncClient::with_uri_str(uri))?;
69+
pub fn with_uri_str(uri: impl AsRef<str>) -> Result<Self> {
70+
let async_client = RUNTIME.block_on(AsyncClient::with_uri_str(uri.as_ref()))?;
7171
Ok(Self { async_client })
7272
}
7373

src/sync/coll.rs

+8-7
Original file line numberDiff line numberDiff line change
@@ -280,27 +280,28 @@ where
280280
/// Finds the distinct values of the field specified by `field_name` across the collection.
281281
pub fn distinct(
282282
&self,
283-
field_name: &str,
283+
field_name: impl AsRef<str>,
284284
filter: impl Into<Option<Document>>,
285285
options: impl Into<Option<DistinctOptions>>,
286286
) -> Result<Vec<Bson>> {
287-
RUNTIME.block_on(
288-
self.async_collection
289-
.distinct(field_name, filter.into(), options.into()),
290-
)
287+
RUNTIME.block_on(self.async_collection.distinct(
288+
field_name.as_ref(),
289+
filter.into(),
290+
options.into(),
291+
))
291292
}
292293

293294
/// Finds the distinct values of the field specified by `field_name` across the collection using
294295
/// the provided `ClientSession`.
295296
pub fn distinct_with_session(
296297
&self,
297-
field_name: &str,
298+
field_name: impl AsRef<str>,
298299
filter: impl Into<Option<Document>>,
299300
options: impl Into<Option<DistinctOptions>>,
300301
session: &mut ClientSession,
301302
) -> Result<Vec<Bson>> {
302303
RUNTIME.block_on(self.async_collection.distinct_with_session(
303-
field_name,
304+
field_name.as_ref(),
304305
filter.into(),
305306
options.into(),
306307
session,

src/sync/db.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -191,10 +191,13 @@ impl Database {
191191
/// not needed if no special options are required.
192192
pub fn create_collection(
193193
&self,
194-
name: &str,
194+
name: impl AsRef<str>,
195195
options: impl Into<Option<CreateCollectionOptions>>,
196196
) -> Result<()> {
197-
RUNTIME.block_on(self.async_database.create_collection(name, options.into()))
197+
RUNTIME.block_on(
198+
self.async_database
199+
.create_collection(name.as_ref(), options.into()),
200+
)
198201
}
199202

200203
/// Creates a new collection in the database with the given `name` and `options` using the
@@ -204,12 +207,12 @@ impl Database {
204207
/// not needed if no special options are required.
205208
pub fn create_collection_with_session(
206209
&self,
207-
name: &str,
210+
name: impl AsRef<str>,
208211
options: impl Into<Option<CreateCollectionOptions>>,
209212
session: &mut ClientSession,
210213
) -> Result<()> {
211214
RUNTIME.block_on(self.async_database.create_collection_with_session(
212-
name,
215+
name.as_ref(),
213216
options.into(),
214217
session,
215218
))

0 commit comments

Comments
 (0)