Skip to content

Commit ca56821

Browse files
use borrowed deserialization
1 parent f657e3d commit ca56821

File tree

3 files changed

+10
-23
lines changed

3 files changed

+10
-23
lines changed

src/error.rs

-12
Original file line numberDiff line numberDiff line change
@@ -741,18 +741,6 @@ pub enum GridFsFileIdentifier {
741741
Id(Bson),
742742
}
743743

744-
impl From<&str> for GridFsFileIdentifier {
745-
fn from(filename: &str) -> Self {
746-
Self::Filename(filename.into())
747-
}
748-
}
749-
750-
impl From<Bson> for GridFsFileIdentifier {
751-
fn from(id: Bson) -> Self {
752-
Self::Id(id)
753-
}
754-
}
755-
756744
/// Translates ErrorKind::BulkWriteError cases to ErrorKind::WriteErrors, leaving all other errors
757745
/// untouched.
758746
pub(crate) fn convert_bulk_errors(error: Error) -> Error {

src/gridfs.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use serde::{Deserialize, Serialize};
1414
use tokio::io::ReadBuf;
1515

1616
use crate::{
17-
bson::{doc, oid::ObjectId, Binary, Bson, DateTime, Document},
17+
bson::{doc, oid::ObjectId, Bson, DateTime, Document, RawBinaryRef},
1818
concern::{ReadConcern, WriteConcern},
1919
cursor::Cursor,
2020
error::Result,
@@ -30,13 +30,13 @@ pub const DEFAULT_CHUNK_SIZE_BYTES: u32 = 255 * 1024;
3030

3131
// Contained in a "chunks" collection for each user file
3232
#[derive(Debug, Deserialize, Serialize)]
33-
struct Chunk {
33+
struct Chunk<'a> {
3434
#[serde(rename = "_id")]
3535
id: ObjectId,
3636
files_id: Bson,
3737
n: u32,
38-
// default size is 255 KiB
39-
data: Binary,
38+
#[serde(borrow)]
39+
data: RawBinaryRef<'a>,
4040
}
4141

4242
/// A collection in which information about stored files is stored. There will be one files
@@ -59,7 +59,7 @@ pub struct FilesCollectionDocument {
5959
struct GridFsBucketInner {
6060
options: GridFsBucketOptions,
6161
files: Collection<FilesCollectionDocument>,
62-
chunks: Collection<Chunk>,
62+
chunks: Collection<Chunk<'static>>,
6363
created_indexes: AtomicBool,
6464
}
6565

src/gridfs/download.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use tokio_util::compat::FuturesAsyncWriteCompatExt;
44
use super::{options::GridFsDownloadByNameOptions, FilesCollectionDocument, GridFsBucket};
55
use crate::{
66
bson::{doc, Bson},
7-
error::{ErrorKind, GridFsErrorKind, Result},
7+
error::{ErrorKind, GridFsErrorKind, GridFsFileIdentifier, Result},
88
options::{FindOneOptions, FindOptions},
99
};
1010

@@ -29,7 +29,7 @@ impl GridFsBucket {
2929
Some(fcd) => fcd,
3030
None => {
3131
return Err(ErrorKind::GridFs(GridFsErrorKind::FileNotFound {
32-
identifier: id.into(),
32+
identifier: GridFsFileIdentifier::Id(id),
3333
})
3434
.into());
3535
}
@@ -92,7 +92,7 @@ impl GridFsBucket {
9292
);
9393
} else {
9494
return Err(ErrorKind::GridFs(GridFsErrorKind::FileNotFound {
95-
identifier: filename.as_ref().into(),
95+
identifier: GridFsFileIdentifier::Filename(filename.as_ref().into()),
9696
})
9797
.into());
9898
}
@@ -141,8 +141,7 @@ impl GridFsBucket {
141141
.selection_criteria(self.selection_criteria().cloned())
142142
.build();
143143
let mut cursor = self
144-
.inner
145-
.chunks
144+
.chunks()
146145
.find(doc! { "files_id": &file.id }, options)
147146
.await?;
148147

@@ -164,7 +163,7 @@ impl GridFsBucket {
164163
.into());
165164
}
166165

167-
destination.write_all(&chunk.data.bytes).await?;
166+
destination.write_all(chunk.data.bytes).await?;
168167
n += 1;
169168
}
170169

0 commit comments

Comments
 (0)