1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
//! Abstraction of a file- or block derive-like object, data from/to which can be read/written at offsets.
//! 
//! There are alreay some analogues of those traits, including in libstd.
//! But they are either platform-specific or tied to implementation of some algorithm.
//! 
//! This crate focuses on the abstraction itself, providing mostly wrappers and helper functions.
//! 
//! Traits are given in two varieties: with mutable `&mut self` and immutable `&self` methods.
//! 
//! libstd's platform-specific FileExt traits are forwarded for std::fs::File.
//! 
//! There is a generic wrapper for using `Read+Seek` or `Read+Write+Seek` objects
//! 
//! Immutable version of traits are implemented for `RefCell`s or `Mutex`s over mutable versions.
//! You may need to use `DerefWrapper` it you use trait ojects although.
//! 
//! TODO:
//! 
//! * vectored IO
//! * async?
//! * reading to uninitialized buffers?
//! * `bytes` crate intergration?

#![forbid(unsafe_code)]
#![deny(missing_docs)]

use std::io::{Read,Write,Seek,SeekFrom,Result,Error,ErrorKind};

/// Read-only generalisation of [`std::os::unix::fs::FileExt`](https://doc.rust-lang.org/stable/std/os/unix/fs/trait.FileExt.html)
pub trait ReadAt {
    /// Reads a number of bytes starting from a given offset.
    /// Returns the number of bytes read.
    /// The offset is relative to the start of the (virtual) file and thus independent
    /// from the current cursor, if the object has concept of a cursor.
    /// That cursor then should not be affected by this function.
    /// Short reads are not considered as errors.
    fn read_at(&self, buf: &mut [u8], offset: u64) -> Result<usize>;

    /// Similar to `read_at`, but without short reads.
    // Implementation is copied from `https://doc.rust-lang.org/stable/src/std/sys/unix/ext/fs.rs.html` in 2020-06-22.
    fn read_exact_at(&self, mut buf: &mut [u8], mut offset: u64) -> Result<()> {
        while !buf.is_empty() {
            match self.read_at(buf, offset) {
                Ok(0) => break,
                Ok(n) => {
                    let tmp = buf;
                    buf = &mut tmp[n..];
                    offset += n as u64;
                }
                Err(ref e) if e.kind() == ErrorKind::Interrupted => {}
                Err(e) => return Err(e),
            }
        }
        if !buf.is_empty() {
            Err(Error::new(ErrorKind::UnexpectedEof, "failed to fill whole buffer"))
        } else {
            Ok(())
        }
    }
}
/// Similar to `ReadAt`, but functions may allow to change object state,
/// including cursor moves if the object has concept of a cursor
/// 
/// Note that `ReadAtMut` implementations from `RefCell` and `Mutex` do not check for cursor moves.
pub trait ReadAtMut {
    /// Similar to `ReadAt::read_at`, but it is allowed to change object internal state.
    fn read_at(&mut self, buf: &mut [u8], offset: u64) -> Result<usize>;

    /// Similar to `read_at`, but without short reads.
    // Implementation is copied from `https://doc.rust-lang.org/stable/src/std/sys/unix/ext/fs.rs.html` in 2020-06-22.
    fn read_exact_at(&mut self, mut buf: &mut [u8], mut offset: u64) -> Result<()> {
        while !buf.is_empty() {
            match self.read_at(buf, offset) {
                Ok(0) => break,
                Ok(n) => {
                    let tmp = buf;
                    buf = &mut tmp[n..];
                    offset += n as u64;
                }
                Err(ref e) if e.kind() == ErrorKind::Interrupted => {}
                Err(e) => return Err(e),
            }
        }
        if !buf.is_empty() {
            Err(Error::new(ErrorKind::UnexpectedEof, "failed to fill whole buffer"))
        } else {
            Ok(())
        }
    }
}

impl<T: ReadAt+?Sized> ReadAtMut for T{ 
    fn read_at(&mut self, buf: &mut [u8], offset: u64) -> Result<usize> {
        ReadAt::read_at(self, buf, offset)
    }
    fn read_exact_at(&mut self, buf: &mut [u8], offset: u64) -> Result<()> {
        ReadAt::read_exact_at(self, buf, offset)
    }
}

/// Write counterpart of `ReadAt`.
pub trait WriteAt {
    /// Writes data contained in buffer `buf` at offset `offset`. May actually write less bytes than you request.
    /// Obviously, it is expected to change information referenced by this object despite of accepting `&self`,
    /// but properties of the writer itself (such as current position, if one exist) should not be changed.
    fn write_at(&self, buf: &[u8], offset: u64) -> Result<usize>;

    /// Similar to `write_at`, but without short writes.
    // Implementation is copied from `https://doc.rust-lang.org/stable/src/std/sys/unix/ext/fs.rs.html` in 2020-06-22.
    fn write_all_at(&self, mut buf: &[u8], mut offset: u64) -> Result<()> {
        while !buf.is_empty() {
            match self.write_at(buf, offset) {
                Ok(0) => {
                    return Err(Error::new(
                        ErrorKind::WriteZero,
                        "failed to write whole buffer",
                    ));
                }
                Ok(n) => {
                    buf = &buf[n..];
                    offset += n as u64
                }
                Err(ref e) if e.kind() == ErrorKind::Interrupted => {}
                Err(e) => return Err(e),
            }
        }
        Ok(())
    }
}
/// Similar to `WriteAt`, but functions may allow to change object state,
/// including cursor moves if the object has concept of a cursor.
/// 
/// Note that `WriteAtMut` implementations from `RefCell` and `Mutex` do not check for cursor moves.
pub trait WriteAtMut {
    /// Writes a number of bytes starting from a given offset.
    /// Returns the number of bytes written.
    /// The offset is relative to the start of the (virtual) file and thus independent
    /// from the current cursor, if the object has concept of a cursor.
    /// That cursor then should not be affected by this function.
    /// Short writes are not considered as errors.
    fn write_at(&mut self, buf: &[u8], offset: u64) -> Result<usize>;

    /// Similar to `write_at`, but without short writes. if entirety of the provided buffer cannot be written,
    /// an error is returned.
    // Implementation is copied from `https://doc.rust-lang.org/stable/src/std/sys/unix/ext/fs.rs.html` in 2020-06-22.
    fn write_all_at(&mut self, mut buf: &[u8], mut offset: u64) -> Result<()> {
        while !buf.is_empty() {
            match self.write_at(buf, offset) {
                Ok(0) => {
                    return Err(Error::new(
                        ErrorKind::WriteZero,
                        "failed to write whole buffer",
                    ));
                }
                Ok(n) => {
                    buf = &buf[n..];
                    offset += n as u64
                }
                Err(ref e) if e.kind() == ErrorKind::Interrupted => {}
                Err(e) => return Err(e),
            }
        }
        Ok(())
    }
}

impl<T: WriteAt+?Sized> WriteAtMut for T{ 
    fn write_at(&mut self, buf: &[u8], offset: u64) -> Result<usize> {
        WriteAt::write_at(self, buf, offset)
    }
}


/// A combined ReadAt and WriteAt for trait objects.
pub trait ReadWriteAt : ReadAt + WriteAt {}
impl<T:ReadAt+WriteAt> ReadWriteAt for T {}

/// A combined ReadAtMut and WriteAtMut for trait objects
pub trait ReadWriteAtMut : ReadAtMut + WriteAtMut {}
impl<T:ReadAtMut+WriteAtMut> ReadWriteAtMut for T {}


// cfg line is copied from https://doc.rust-lang.org/stable/src/std/os/mod.rs.html at 2020-06-22
#[cfg(any(target_os = "redox", unix, target_os = "vxworks", target_os = "hermit"))]
impl WriteAt for std::fs::File {
    fn write_at(&self, buf: &[u8], offset: u64) -> Result<usize> {
        std::os::unix::fs::FileExt::write_at(self, buf, offset)
    }
    fn write_all_at(&self, buf: &[u8], offset: u64) -> Result<()> {
        std::os::unix::fs::FileExt::write_all_at(self, buf, offset)
    }
}

// cfg line is copied from https://doc.rust-lang.org/stable/src/std/os/mod.rs.html at 2020-06-22
#[cfg(any(target_os = "redox", unix, target_os = "vxworks", target_os = "hermit"))]
impl ReadAt for std::fs::File {
    fn read_at(&self, buf: &mut [u8], offset: u64) -> Result<usize> {
        std::os::unix::fs::FileExt::read_at(self, buf, offset)
    }
    fn read_exact_at(&self, buf: &mut [u8], offset: u64) -> Result<()> {
        std::os::unix::fs::FileExt::read_exact_at(self, buf, offset)
    }
}

#[cfg(windows)]
/// Note that cursor is affected. That why it's `WriteAtMut` instead of `WriteAt`
impl WriteAtMut for std::fs::File {
    fn write_at(&mut self, buf: &[u8], offset: u64) -> Result<usize> {
        std::os::windows::fs::FileExt::seek_write(self, buf, offset)
    }
}
#[cfg(windows)]
/// Note that cursor is affected. That why it's `ReadAtMut` instead of `ReadAt`
impl ReadAtMut for std::fs::File {
    fn read_at(&mut self, buf: &mut [u8], offset: u64) -> Result<usize> {
        std::os::windows::fs::FileExt::seek_read(self, buf, offset)
    }
}

/// A wrapper that calls `Seek::seek` and `Read::read` or `Write::write` for each call of `read_at` or `write_at`
/// Can be used for read-only access as well.
/// 
/// Example:
/// 
/// ```
/// use read_write_at::{ReadWriteSeek,ReadAtMut};
/// 
/// let v = vec![3u8, 4,5,6];
/// let c = std::io::Cursor::new(v);
/// let mut rws = ReadWriteSeek(c);
/// 
/// let mut v2 = vec![0,0];
/// rws.read_exact_at(&mut v2[..], 1).unwrap();
/// assert_eq!(v2, vec![4,5]);
/// ```
pub struct ReadWriteSeek<T:Seek>(pub T);

impl<T:Read+Seek> ReadAtMut for ReadWriteSeek<T> {
    fn read_at(&mut self, buf: &mut [u8], offset: u64) -> Result<usize> {
        let o = Seek::seek(&mut self.0, SeekFrom::Start(offset))?;
        if o != offset {
            return Err(Error::new(
                ErrorKind::UnexpectedEof,
                "seek hasn't returned the required offset",
            ));
        }
        Read::read(&mut self.0, buf)
    }
    fn read_exact_at(&mut self, buf: &mut [u8], offset: u64) -> Result<()> {
        let o = Seek::seek(&mut self.0, SeekFrom::Start(offset))?;
        if o != offset {
            return Err(Error::new(
                ErrorKind::UnexpectedEof,
                "seek hasn't returned the required offset",
            ));
        }
        Read::read_exact(&mut self.0, buf)
    }
}

impl<T:Write+Seek> WriteAtMut for ReadWriteSeek<T> {
    fn write_at(&mut self, buf: &[u8], offset: u64) -> Result<usize> {
        let o = Seek::seek(&mut self.0, SeekFrom::Start(offset))?;
        if o != offset {
            return Err(Error::new(
                ErrorKind::UnexpectedEof,
                "seek hasn't returned the required offset",
            ));
        }
        Write::write(&mut self.0, buf)
    }
    fn write_all_at(&mut self, buf: &[u8], offset: u64) -> Result<()> {
        let o = Seek::seek(&mut self.0, SeekFrom::Start(offset))?;
        if o != offset {
            return Err(Error::new(
                ErrorKind::UnexpectedEof,
                "seek hasn't returned the required offset",
            ));
        }
        Write::write_all(&mut self.0, buf)
    }
}


/// A wrapper struct to allow accessing `RefCell` and `Mutex` helper impls for trait objects.
///
/// Example:
/// 
/// ```
/// use read_write_at::{ReadWriteSeek,ReadWriteAtMut,ReadWriteAt,DerefWrapper};
/// 
/// let v = vec![3u8, 4,5,6];
/// let c = std::io::Cursor::new(v);
/// let rws = ReadWriteSeek(c);
/// let obj1 : Box<dyn ReadWriteAtMut> = Box::new(rws);
/// let mtx = std::sync::Mutex::new(DerefWrapper(obj1));
/// let obj2 : Box<dyn ReadWriteAt> = Box::new(mtx);
/// 
/// let mut v2 = vec![0,0];
/// obj2.read_exact_at(&mut v2[..], 1).unwrap();
/// assert_eq!(v2, vec![4,5]);
/// ```
pub struct DerefWrapper<T: std::ops::DerefMut> (pub T);

impl<T,U> ReadAtMut for DerefWrapper<U>
where T:ReadAtMut+?Sized, U: std::ops::DerefMut<Target = T>
{
    fn read_at(&mut self, buf: &mut [u8], offset: u64) -> Result<usize> {
        ReadAtMut::read_at(std::ops::DerefMut::deref_mut(&mut self.0), buf, offset)
    }
    fn read_exact_at(&mut self, buf: &mut [u8], offset: u64) -> Result<()> {
        ReadAtMut::read_exact_at(std::ops::DerefMut::deref_mut(&mut self.0), buf, offset)
    }
}

impl<T,U> WriteAtMut for DerefWrapper<U>
where T:WriteAtMut+?Sized, U: std::ops::DerefMut<Target = T>
{
    fn write_at(&mut self, buf: &[u8], offset: u64) -> Result<usize> {
        WriteAtMut::write_at(std::ops::DerefMut::deref_mut (&mut self.0), buf, offset)
    }
    fn write_all_at(&mut self, buf: &[u8], offset: u64) -> Result<()> {
        WriteAtMut::write_all_at(std::ops::DerefMut::deref_mut(&mut self.0), buf, offset)
    }
}



impl<T> ReadAt for std::cell::RefCell<T> 
where T:ReadAtMut+?Sized
{
    fn read_at(&self, buf: &mut [u8], offset: u64) -> Result<usize> {
        let mut se = self.borrow_mut();
        ReadAtMut::read_at(&mut *se, buf, offset)
    }
    fn read_exact_at(&self, buf: &mut [u8], offset: u64) -> Result<()> {
        let mut se = self.borrow_mut();
        ReadAtMut::read_exact_at(&mut *se, buf, offset)
    }
}

impl<T> WriteAt for std::cell::RefCell<T> 
where T:WriteAtMut+?Sized
{
    fn write_at(&self, buf: &[u8], offset: u64) -> Result<usize> {
        let mut se = self.borrow_mut();
        WriteAtMut::write_at(&mut *se, buf, offset)
    }
    fn write_all_at(&self, buf: &[u8], offset: u64) -> Result<()> {
        let mut se = self.borrow_mut();
        WriteAtMut::write_all_at(&mut *se, buf, offset)
    }
}



impl<T> ReadAt for std::sync::Mutex<T> 
where T:ReadAtMut+?Sized
{
    fn read_at(&self, buf: &mut [u8], offset: u64) -> Result<usize> {
        let se = self.lock();
        let mut se = match se {
            Ok(x) => x,
            Err(_) =>  return Err(Error::new(
                ErrorKind::Other,
                "poisoned mutex encountered",
            )),
        };
        ReadAtMut::read_at(&mut *se, buf, offset)
    }
    fn read_exact_at(&self, buf: &mut [u8], offset: u64) -> Result<()> {
        let se = self.lock();
        let mut se = match se {
            Ok(x) => x,
            Err(_) =>  return Err(Error::new(
                ErrorKind::Other,
                "poisoned mutex encountered",
            )),
        };
        ReadAtMut::read_exact_at(&mut *se, buf, offset)
    }
}

impl<T> WriteAt for std::sync::Mutex<T> 
where T:WriteAtMut+?Sized
{
    fn write_at(&self, buf: &[u8], offset: u64) -> Result<usize> {
        let se = self.lock();
        let mut se = match se {
            Ok(x) => x,
            Err(_) =>  return Err(Error::new(
                ErrorKind::Other,
                "poisoned mutex encountered",
            )),
        };
        WriteAtMut::write_at(&mut *se, buf, offset)
    }
    fn write_all_at(&self, buf: &[u8], offset: u64) -> Result<()> {
        let se = self.lock();
        let mut se = match se {
            Ok(x) => x,
            Err(_) =>  return Err(Error::new(
                ErrorKind::Other,
                "poisoned mutex encountered",
            )),
        };
        WriteAtMut::write_all_at(&mut *se, buf, offset)
    }
}


//pub struct DerefWrapper

#[cfg(test)]
mod tests {
    use super::*;

    fn i_want_immut<T:ReadAt+?Sized>(t:&T) {
        let mut v = vec![0,0,0];
        t.read_exact_at(&mut v[..], 3).unwrap();
        assert_eq!(v, vec![7,8,9]);
    }
    fn i_want_mut<T:ReadAtMut+?Sized>(t:&mut T) {
        let mut v = vec![0,0];
        t.read_exact_at(&mut v[..], 2).unwrap();
        assert_eq!(v, vec![6,7]);
    }
    fn i_have_obj() -> Box<dyn ReadAtMut> { 
        let v = vec![4u8, 5,6,7,8,9,10,11];
        let o = ReadWriteSeek(std::io::Cursor::new(v));
        Box::new(o)
     }

    #[test]
    fn check_refc_wrapping_works() {
        let mut o = i_have_obj();
        i_want_mut(&mut *o);
        let rc = std::cell::RefCell::new(DerefWrapper(o));
        i_want_immut(&rc);

        let v = vec![4u8, 5,6,7,8,9,10,11];
        let mut o2 = ReadWriteSeek(std::io::Cursor::new(v));
        i_want_mut(&mut o2);
        let rc2 = std::cell::RefCell::new(o2);
        i_want_immut(&rc2);
    }


    fn i_have_obj2() -> Box<dyn ReadWriteAtMut + Send> { 
        let v = vec![4u8, 5,6,7,8,9,10,11];
        let o = ReadWriteSeek(std::io::Cursor::new(v));
        Box::new(o)
    }
    fn i_want_immut2<T:ReadWriteAt+?Sized>(t:&T) {
        let mut v = vec![0,0,0];
        t.read_exact_at(&mut v[..], 0).unwrap();
        assert_eq!(v, vec![4,5,6]);

        std::thread::sleep(std::time::Duration::from_millis(100));

        t.read_exact_at(&mut v[..], 0).unwrap();
        assert_eq!(v, vec![4,44,44]);
    }
    fn i_want_immut3<T:ReadWriteAt+?Sized>(t:&T) {
        let v = vec![44,44, 44];
        std::thread::sleep(std::time::Duration::from_millis(50));
        t.write_all_at(&v[..], 1).unwrap();
    }

    #[test]
    fn check_mutex_wrapping_works() {
        let o = i_have_obj2();
        let rc = std::sync::Mutex::new(DerefWrapper(o));
        let rc = std::sync::Arc::new(rc);
        let rc2 = rc.clone();
        
        let g1 = std::thread::spawn(move|| {
            i_want_immut2(&*rc)
        });
        let g2 = std::thread::spawn(move|| {
            i_want_immut3(&*rc2)
        });
        g1.join().unwrap();
        g2.join().unwrap();
    }

    #[allow(unused)]
    #[cfg(unix)]
    fn check_refc_wrapping_works2() {
      
        let f : std::fs::File = unimplemented!();
        i_want_mut(&mut f);
        let rc2 = std::cell::RefCell::new(f);
        i_want_immut(&rc2);
    }
}