@@ -29,10 +29,10 @@ use crate::error::{AiocogeoError, Result};
29
29
/// [`tokio::fs::File`]: https://docs.rs/tokio/latest/tokio/fs/struct.File.html
30
30
pub trait AsyncFileReader : Debug + Send + Sync {
31
31
/// Retrieve the bytes in `range`
32
- fn get_bytes ( & mut self , range : Range < u64 > ) -> BoxFuture < ' _ , Result < Bytes > > ;
32
+ fn get_bytes ( & self , range : Range < u64 > ) -> BoxFuture < ' _ , Result < Bytes > > ;
33
33
34
34
/// Retrieve multiple byte ranges. The default implementation will call `get_bytes` sequentially
35
- fn get_byte_ranges ( & mut self , ranges : Vec < Range < u64 > > ) -> BoxFuture < ' _ , Result < Vec < Bytes > > > {
35
+ fn get_byte_ranges ( & self , ranges : Vec < Range < u64 > > ) -> BoxFuture < ' _ , Result < Vec < Bytes > > > {
36
36
async move {
37
37
let mut result = Vec :: with_capacity ( ranges. len ( ) ) ;
38
38
@@ -49,37 +49,37 @@ pub trait AsyncFileReader: Debug + Send + Sync {
49
49
50
50
/// This allows Box<dyn AsyncFileReader + '_> to be used as an AsyncFileReader,
51
51
impl AsyncFileReader for Box < dyn AsyncFileReader + ' _ > {
52
- fn get_bytes ( & mut self , range : Range < u64 > ) -> BoxFuture < ' _ , Result < Bytes > > {
53
- self . as_mut ( ) . get_bytes ( range)
52
+ fn get_bytes ( & self , range : Range < u64 > ) -> BoxFuture < ' _ , Result < Bytes > > {
53
+ self . as_ref ( ) . get_bytes ( range)
54
54
}
55
55
56
- fn get_byte_ranges ( & mut self , ranges : Vec < Range < u64 > > ) -> BoxFuture < ' _ , Result < Vec < Bytes > > > {
57
- self . as_mut ( ) . get_byte_ranges ( ranges)
56
+ fn get_byte_ranges ( & self , ranges : Vec < Range < u64 > > ) -> BoxFuture < ' _ , Result < Vec < Bytes > > > {
57
+ self . as_ref ( ) . get_byte_ranges ( ranges)
58
58
}
59
59
}
60
60
61
- #[ cfg( feature = "tokio" ) ]
62
- impl < T : tokio:: io:: AsyncRead + tokio:: io:: AsyncSeek + Unpin + Debug + Send + Sync > AsyncFileReader
63
- for T
64
- {
65
- fn get_bytes ( & mut self , range : Range < u64 > ) -> BoxFuture < ' _ , Result < Bytes > > {
66
- use tokio:: io:: { AsyncReadExt , AsyncSeekExt } ;
67
-
68
- async move {
69
- self . seek ( std:: io:: SeekFrom :: Start ( range. start ) ) . await ?;
70
-
71
- let to_read = ( range. end - range. start ) . try_into ( ) . unwrap ( ) ;
72
- let mut buffer = Vec :: with_capacity ( to_read) ;
73
- let read = self . take ( to_read as u64 ) . read_to_end ( & mut buffer) . await ?;
74
- if read != to_read {
75
- return Err ( AiocogeoError :: EndOfFile ( to_read, read) ) ;
76
- }
77
-
78
- Ok ( buffer. into ( ) )
79
- }
80
- . boxed ( )
81
- }
82
- }
61
+ // #[cfg(feature = "tokio")]
62
+ // impl<T: tokio::io::AsyncRead + tokio::io::AsyncSeek + Unpin + Debug + Send + Sync> AsyncFileReader
63
+ // for T
64
+ // {
65
+ // fn get_bytes(&self, range: Range<u64>) -> BoxFuture<'_, Result<Bytes>> {
66
+ // use tokio::io::{AsyncReadExt, AsyncSeekExt};
67
+
68
+ // async move {
69
+ // self.seek(std::io::SeekFrom::Start(range.start)).await?;
70
+
71
+ // let to_read = (range.end - range.start).try_into().unwrap();
72
+ // let mut buffer = Vec::with_capacity(to_read);
73
+ // let read = self.take(to_read as u64).read_to_end(&mut buffer).await?;
74
+ // if read != to_read {
75
+ // return Err(AiocogeoError::EndOfFile(to_read, read));
76
+ // }
77
+
78
+ // Ok(buffer.into())
79
+ // }
80
+ // .boxed()
81
+ // }
82
+ // }
83
83
84
84
#[ derive( Clone , Debug ) ]
85
85
pub struct ObjectReader {
@@ -97,14 +97,14 @@ impl ObjectReader {
97
97
}
98
98
99
99
impl AsyncFileReader for ObjectReader {
100
- fn get_bytes ( & mut self , range : Range < u64 > ) -> BoxFuture < ' _ , Result < Bytes > > {
100
+ fn get_bytes ( & self , range : Range < u64 > ) -> BoxFuture < ' _ , Result < Bytes > > {
101
101
self . store
102
102
. get_range ( & self . path , range)
103
103
. map_err ( |e| e. into ( ) )
104
104
. boxed ( )
105
105
}
106
106
107
- fn get_byte_ranges ( & mut self , ranges : Vec < Range < u64 > > ) -> BoxFuture < ' _ , Result < Vec < Bytes > > >
107
+ fn get_byte_ranges ( & self , ranges : Vec < Range < u64 > > ) -> BoxFuture < ' _ , Result < Vec < Bytes > > >
108
108
where
109
109
Self : Send ,
110
110
{
@@ -125,14 +125,14 @@ pub struct PrefetchReader {
125
125
}
126
126
127
127
impl PrefetchReader {
128
- pub async fn new ( mut reader : Box < dyn AsyncFileReader > , prefetch : u64 ) -> Result < Self > {
128
+ pub async fn new ( reader : Box < dyn AsyncFileReader > , prefetch : u64 ) -> Result < Self > {
129
129
let buffer = reader. get_bytes ( 0 ..prefetch) . await ?;
130
130
Ok ( Self { reader, buffer } )
131
131
}
132
132
}
133
133
134
134
impl AsyncFileReader for PrefetchReader {
135
- fn get_bytes ( & mut self , range : Range < u64 > ) -> BoxFuture < ' _ , Result < Bytes > > {
135
+ fn get_bytes ( & self , range : Range < u64 > ) -> BoxFuture < ' _ , Result < Bytes > > {
136
136
if range. start < self . buffer . len ( ) as _ {
137
137
if range. end < self . buffer . len ( ) as _ {
138
138
let usize_range = range. start as usize ..range. end as usize ;
@@ -147,7 +147,7 @@ impl AsyncFileReader for PrefetchReader {
147
147
}
148
148
}
149
149
150
- fn get_byte_ranges ( & mut self , ranges : Vec < Range < u64 > > ) -> BoxFuture < ' _ , Result < Vec < Bytes > > >
150
+ fn get_byte_ranges ( & self , ranges : Vec < Range < u64 > > ) -> BoxFuture < ' _ , Result < Vec < Bytes > > >
151
151
where
152
152
Self : Send ,
153
153
{
0 commit comments