-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
replace file system with file builder (#96)
Replace file system (#91) with an Allocator-like type `FileBuilder`. File builder is better, it's strongly typed with our own file writer/reader, so that 1) we don't need to propagate `Send` and `Sync` trait bound outside of this library, 2) user writers or readers are promised to use our writer/reader as a backend. There still are some reasonable requirements on user types, such as no buffering and no length altering, which are documented in trait definition. Signed-off-by: tabokie <[email protected]>
- Loading branch information
Showing
11 changed files
with
305 additions
and
280 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Copyright (c) 2017-present, PingCAP, Inc. Licensed under Apache-2.0. | ||
|
||
use std::io::{Read, Seek, Write}; | ||
use std::path::Path; | ||
|
||
/// A `FileBuilder` generates file readers or writers that are composed upon | ||
/// existing ones. | ||
pub trait FileBuilder: Send + Sync { | ||
/// Types of outcome file reader/writer. They must not alter the length of | ||
/// bytes stream, nor buffer bytes between operations. | ||
type Reader<R: Seek + Read>: Seek + Read; | ||
type Writer<W: Seek + Write>: Seek + Write; | ||
|
||
fn build_reader<R>( | ||
&self, | ||
path: &Path, | ||
reader: R, | ||
) -> Result<Self::Reader<R>, Box<dyn std::error::Error>> | ||
where | ||
R: Seek + Read; | ||
|
||
fn build_writer<W>( | ||
&self, | ||
path: &Path, | ||
writer: W, | ||
create: bool, | ||
) -> Result<Self::Writer<W>, Box<dyn std::error::Error>> | ||
where | ||
W: Seek + Write; | ||
} | ||
|
||
/// `DefaultFileBuilder` is a `FileBuilder` that builds out the original | ||
/// `Reader`/`Writer` as it is. | ||
pub struct DefaultFileBuilder {} | ||
|
||
impl FileBuilder for DefaultFileBuilder { | ||
type Reader<R: Seek + Read> = R; | ||
type Writer<W: Seek + Write> = W; | ||
|
||
fn build_reader<R>( | ||
&self, | ||
_path: &Path, | ||
reader: R, | ||
) -> Result<Self::Reader<R>, Box<dyn std::error::Error>> | ||
where | ||
R: Seek + Read, | ||
{ | ||
Ok(reader) | ||
} | ||
|
||
fn build_writer<W>( | ||
&self, | ||
_path: &Path, | ||
writer: W, | ||
_create: bool, | ||
) -> Result<Self::Writer<W>, Box<dyn std::error::Error>> | ||
where | ||
W: Seek + Write, | ||
{ | ||
Ok(writer) | ||
} | ||
} |
Oops, something went wrong.