off64/
lib.rs

1use async_trait::async_trait;
2
3#[cfg(feature = "chrono")]
4pub mod chrono;
5pub mod file;
6pub mod int;
7pub mod range;
8pub mod slice;
9#[cfg(test)]
10pub mod test;
11
12// The read trait method has a generic return type so the implementer can return borrowed (e.g. `&[u8]`) or owned (e.g. `Vec<u8>`) value depending on their requirements, and still be able to use all Off64 extension/helper traits and methods without any overhead.
13// Note that the lifetime is associated with the lifetime parameter (i.e. `T: 'a`), instead of requiring a borrow with the lifetime as the return type (i.e. `&'a T`). This allows returning both owned (with no lifetime) and borrowed (with a lifetime) values.
14
15pub trait Off64Read<'a, T: 'a + AsRef<[u8]>> {
16  fn read_at(&'a self, offset: u64, len: u64) -> T;
17}
18
19#[async_trait]
20pub trait Off64AsyncRead<'a, T: 'a + AsRef<[u8]>> {
21  async fn read_at(&self, offset: u64, len: u64) -> T;
22}
23
24// The write traits do not take a generic `AsRef<[u8]>` value as the only (main) users of the trait method, the various Off64 extension/helper traits and methods, only ever write slices, so it would not be useful. If the implementation requires an owned value, clone the slice into a Vec; there's no *unnecessary* overhead as the value was not originally in the heap, so it's not a pointless double allocation. If a `write_at` method that takes an owned value is desired (e.g. to signal that an owned copy is required/preferred to avoid cloning), then just implement it as a regular method; Off64 does not require such a method and won't use it anyway, and other callers will simply call the method directly or have their own traits.
25
26pub trait Off64Write {
27  fn write_at(&self, offset: u64, value: &[u8]) -> ();
28}
29
30pub trait Off64WriteMut {
31  fn write_at(&mut self, offset: u64, value: &[u8]) -> ();
32}
33
34#[async_trait]
35pub trait Off64AsyncWrite {
36  async fn write_at(&self, offset: u64, value: &[u8]) -> ();
37}
38
39#[async_trait]
40pub trait Off64AsyncWriteMut {
41  async fn write_at(&mut self, offset: u64, value: &[u8]) -> ();
42}