Skip to content
This repository was archived by the owner on Oct 18, 2021. It is now read-only.

Commit 6aaf8a3

Browse files
committed
change .to_owned() to String::from where possible
1 parent 185be95 commit 6aaf8a3

File tree

26 files changed

+125
-133
lines changed

26 files changed

+125
-133
lines changed

src/auth.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -79,17 +79,17 @@ impl Authenticator {
7979

8080
let data = match doc.get("payload") {
8181
Some(&Binary(_, ref payload)) => payload.to_owned(),
82-
_ => return Err(ResponseError("Invalid payload returned".to_owned())),
82+
_ => return Err(ResponseError(String::from("Invalid payload returned"))),
8383
};
8484

8585
let id = match doc.get("conversationId") {
8686
Some(bson) => bson.clone(),
87-
None => return Err(ResponseError("No conversationId returned".to_owned())),
87+
None => return Err(ResponseError(String::from("No conversationId returned"))),
8888
};
8989

9090
let response = match String::from_utf8(data) {
9191
Ok(string) => string,
92-
Err(_) => return Err(ResponseError("Invalid UTF-8 payload returned".to_owned())),
92+
Err(_) => return Err(ResponseError(String::from("Invalid UTF-8 payload returned"))),
9393
};
9494

9595
Ok(InitialData {
@@ -110,7 +110,7 @@ impl Authenticator {
110110

111111
let rnonce_b64 = match rnonce_opt {
112112
Some(val) => val,
113-
None => return Err(ResponseError("Invalid rnonce returned".to_owned())),
113+
None => return Err(ResponseError(String::from("Invalid rnonce returned"))),
114114
};
115115

116116
// Validate rnonce to make sure server isn't malicious
@@ -120,18 +120,18 @@ impl Authenticator {
120120

121121
let salt_b64 = match salt_opt {
122122
Some(val) => val,
123-
None => return Err(ResponseError("Invalid salt returned".to_owned())),
123+
None => return Err(ResponseError(String::from("Invalid salt returned"))),
124124
};
125125

126126
let salt = match salt_b64.from_base64() {
127127
Ok(val) => val,
128-
Err(_) => return Err(ResponseError("Invalid base64 salt returned".to_owned())),
128+
Err(_) => return Err(ResponseError(String::from("Invalid base64 salt returned"))),
129129
};
130130

131131

132132
let i = match i_opt {
133133
Some(val) => val,
134-
None => return Err(ResponseError("Invalid iteration count returned".to_owned())),
134+
None => return Err(ResponseError(String::from("Invalid iteration count returned"))),
135135
};
136136

137137
// Hash password
@@ -170,8 +170,8 @@ impl Authenticator {
170170

171171
// Sanity check
172172
if client_key.len() != client_signature.len() {
173-
return Err(DefaultError("Generated client key and/or client signature is invalid"
174-
.to_owned()));
173+
return Err(DefaultError(String::from("Generated client key and/or client signature \
174+
is invalid")));
175175
}
176176

177177
// Compute proof by xor'ing key and signature
@@ -226,7 +226,7 @@ impl Authenticator {
226226
let payload_str = match String::from_utf8(payload.to_owned()) {
227227
Ok(string) => string,
228228
Err(_) => {
229-
return Err(ResponseError("Invalid UTF-8 payload returned".to_owned()))
229+
return Err(ResponseError(String::from("Invalid UTF-8 payload returned")))
230230
}
231231
};
232232

src/coll/error.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ impl WriteException {
134134
pub fn new(wc_err: Option<WriteConcernError>, w_err: Option<WriteError>) -> WriteException {
135135
let mut s = match wc_err {
136136
Some(ref error) => format!("{:?}\n", error),
137-
None => "".to_owned(),
137+
None => String::from(""),
138138
};
139139

140140
if let Some(ref error) = w_err {
@@ -144,7 +144,7 @@ impl WriteException {
144144
WriteException {
145145
write_concern_error: wc_err,
146146
write_error: w_err,
147-
message: s.to_owned(),
147+
message: String::from(s),
148148
}
149149
}
150150

@@ -257,7 +257,7 @@ impl BulkWriteException {
257257

258258
let mut s = match write_concern_error {
259259
Some(ref error) => format!("{:?}\n", error),
260-
None => "".to_owned(),
260+
None => String::from(""),
261261
};
262262

263263
for v in &write_errors {
@@ -269,7 +269,7 @@ impl BulkWriteException {
269269
unprocessed_requests: unprocessed,
270270
write_concern_error: write_concern_error,
271271
write_errors: write_errors,
272-
message: s.to_owned(),
272+
message: String::from(s),
273273
}
274274
}
275275

@@ -332,19 +332,17 @@ impl BulkWriteException {
332332
// Parse out any write errors.
333333
let w_errs = if let Some(&Bson::Array(ref errors)) = result.get("writeErrors") {
334334
if errors.is_empty() {
335-
return Err(Error::ResponseError("Server indicates a write error, but none were \
336-
found."
337-
.to_owned()));
335+
return Err(Error::ResponseError(String::from("Server indicates a write error, \
336+
but none were found.")));
338337
}
339338

340339
let mut vec = Vec::new();
341340
for err in errors {
342341
if let Bson::Document(ref doc) = *err {
343342
vec.push(try!(BulkWriteError::parse(doc.clone())));
344343
} else {
345-
return Err(Error::ResponseError("WriteError provided was not a bson \
346-
document."
347-
.to_owned()));
344+
return Err(Error::ResponseError(String::from("WriteError provided was not \
345+
a bson document.")));
348346
}
349347
}
350348
vec

src/coll/mod.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,12 @@ impl Collection {
7171
pub fn name(&self) -> String {
7272
match self.namespace.find('.') {
7373
Some(idx) => {
74-
self.namespace[self.namespace
75-
.char_indices()
76-
.nth(idx + 1)
77-
.unwrap()
78-
.0..]
79-
.to_owned()
74+
let string = &self.namespace[self.namespace
75+
.char_indices()
76+
.nth(idx + 1)
77+
.unwrap()
78+
.0..];
79+
String::from(string)
8080
}
8181
None => {
8282
// '.' is inserted in Collection::new, so this should only panic due to user error.
@@ -143,7 +143,7 @@ impl Collection {
143143
match result.get("n") {
144144
Some(&Bson::I32(ref n)) => Ok(*n as i64),
145145
Some(&Bson::I64(ref n)) => Ok(*n),
146-
_ => Err(ResponseError("No count received from server.".to_owned())),
146+
_ => Err(ResponseError(String::from("No count received from server."))),
147147
}
148148
}
149149

@@ -158,7 +158,7 @@ impl Collection {
158158

159159
let mut spec = bson::Document::new();
160160
spec.insert("distinct", Bson::String(self.name()));
161-
spec.insert("key", Bson::String(field_name.to_owned()));
161+
spec.insert("key", Bson::String(String::from(field_name)));
162162
if filter.is_some() {
163163
spec.insert("query", Bson::Document(filter.unwrap()));
164164
}
@@ -167,7 +167,7 @@ impl Collection {
167167
let result = try!(self.db.command(spec, CommandType::Distinct, Some(read_pref)));
168168
match result.get("values") {
169169
Some(&Bson::Array(ref vals)) => Ok(vals.to_owned()),
170-
_ => Err(ResponseError("No values received from server.".to_owned())),
170+
_ => Err(ResponseError(String::from("No values received from server."))),
171171
}
172172
}
173173

@@ -189,10 +189,10 @@ impl Collection {
189189

190190
let doc = if options.sort.is_some() {
191191
let mut doc = bson::Document::new();
192-
doc.insert("$query".to_owned(),
192+
doc.insert(String::from("$query"),
193193
Bson::Document(filter.unwrap_or_else(bson::Document::new)));
194194

195-
doc.insert("$orderby".to_owned(),
195+
doc.insert(String::from("$orderby"),
196196
Bson::Document(options.sort.as_ref().unwrap().clone()));
197197

198198
doc
@@ -630,7 +630,7 @@ impl Collection {
630630
CommandType::InsertOne));
631631

632632
if ids.is_empty() {
633-
return Err(OperationError("No ids returned for insert_one.".to_owned()));
633+
return Err(OperationError(String::from("No ids returned for insert_one.")));
634634
}
635635

636636
// Downgrade bulk exception, if it exists.
@@ -859,7 +859,7 @@ impl Collection {
859859
fn validate_replace(replacement: &bson::Document) -> Result<()> {
860860
for key in replacement.keys() {
861861
if key.starts_with('$') {
862-
return Err(ArgumentError("Replacement cannot include $ operators.".to_owned()));
862+
return Err(ArgumentError(String::from("Replacement cannot include $ operators.")));
863863
}
864864
}
865865
Ok(())
@@ -868,7 +868,7 @@ impl Collection {
868868
fn validate_update(update: &bson::Document) -> Result<()> {
869869
for key in update.keys() {
870870
if !key.starts_with('$') {
871-
return Err(ArgumentError("Update only works with $ operators.".to_owned()));
871+
return Err(ArgumentError(String::from("Update only works with $ operators.")));
872872
}
873873
}
874874
Ok(())
@@ -919,7 +919,7 @@ impl Collection {
919919
/// Drop an index by name.
920920
pub fn drop_index_string(&self, name: String) -> Result<()> {
921921
let mut opts = IndexOptions::new();
922-
opts.name = Some(name.to_owned());
922+
opts.name = Some(String::from(name));
923923

924924
let model = IndexModel::new(bson::Document::new(), Some(opts));
925925
self.drop_index_model(model)
@@ -941,7 +941,7 @@ impl Collection {
941941
/// Drop all indexes in the collection.
942942
pub fn drop_indexes(&self) -> Result<()> {
943943
let mut opts = IndexOptions::new();
944-
opts.name = Some("*".to_owned());
944+
opts.name = Some(String::from("*"));
945945

946946
let model = IndexModel::new(bson::Document::new(), Some(opts));
947947
self.drop_index_model(model)

src/coll/options.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ impl IndexModel {
308308
name.push('_');
309309
match *bson {
310310
Bson::I32(ref i) => name.push_str(&format!("{}", i)),
311-
_ => return Err(ArgumentError("Index model keys must map to i32.".to_owned())),
311+
_ => return Err(ArgumentError(String::from("Index model keys must map to i32."))),
312312
}
313313
}
314314
Ok(name)

0 commit comments

Comments
 (0)