Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion core/core/src/types/read/futures_async_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,25 @@ pub struct FuturesAsyncReader {
/// Safety: FuturesAsyncReader only exposes `&mut self` to the outside world,
unsafe impl Sync for FuturesAsyncReader {}

impl std::fmt::Debug for FuturesAsyncReader {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let info = self.ctx.accessor().info();
let root = info.root();

f.debug_struct("FuturesAsyncReader")
.field("scheme", &info.scheme())
.field("root", &root)
.field("path", &self.ctx.path())
.field("args", self.ctx.args())
.field("options", self.ctx.options())
.field("start", &self.start)
.field("end", &self.end)
.field("pos", &self.pos)
.field("buffered", &self.buf.len())
.finish_non_exhaustive()
}
}

impl FuturesAsyncReader {
/// NOTE: don't allow users to create FuturesAsyncReader directly.
///
Expand Down Expand Up @@ -200,8 +219,10 @@ mod tests {
OpReader::new(),
));

let v = FuturesAsyncReader::new(ctx, 4..8);
fn assert_reader_traits<T: std::fmt::Debug + Unpin + MaybeSend + Sync + 'static>(_: &T) {}

let v = FuturesAsyncReader::new(ctx, 4..8);
assert_reader_traits(&v);
let _: Box<dyn Unpin + MaybeSend + Sync + 'static> = Box::new(v);
Ok(())
}
Expand Down
38 changes: 37 additions & 1 deletion core/core/src/types/read/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,21 @@ pub struct Reader {
ctx: Arc<ReadContext>,
}

impl std::fmt::Debug for Reader {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let info = self.ctx.accessor().info();
let root = info.root();

f.debug_struct("Reader")
.field("scheme", &info.scheme())
.field("root", &root)
.field("path", &self.ctx.path())
.field("args", self.ctx.args())
.field("options", self.ctx.options())
.finish_non_exhaustive()
}
}

impl Reader {
/// Create a new reader.
///
Expand Down Expand Up @@ -452,7 +467,28 @@ mod tests {
let acc = op.into_inner();
let ctx = ReadContext::new(acc, "test".to_string(), OpRead::new(), OpReader::new());

let _: Box<dyn Unpin + MaybeSend + Sync + 'static> = Box::new(Reader::new(ctx));
fn assert_reader_traits<T: std::fmt::Debug + Unpin + MaybeSend + Sync + 'static>(_: &T) {}

let reader = Reader::new(ctx);
assert_reader_traits(&reader);
let _: Box<dyn Unpin + MaybeSend + Sync + 'static> = Box::new(reader);

Ok(())
}

#[tokio::test]
async fn test_debug() -> Result<()> {
let op = Operator::via_iter(services::MEMORY_SCHEME, [])?;
op.write("test", "hello").await?;

let reader = op.reader_with("test").chunk(8).await?;
let output = format!("{reader:?}");

assert!(output.contains("Reader"), "{output}");
assert!(output.contains("memory"), "{output}");
assert!(output.contains("test"), "{output}");
assert!(output.contains("args"), "{output}");
assert!(output.contains("options"), "{output}");

Ok(())
}
Expand Down
10 changes: 9 additions & 1 deletion core/core/src/types/write/futures_async_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ pub struct FuturesAsyncWriter {
buf: oio::FlexBuf,
}

impl std::fmt::Debug for FuturesAsyncWriter {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("FuturesAsyncWriter").finish_non_exhaustive()
}
}

impl FuturesAsyncWriter {
/// NOTE: don't allow users to create directly.
#[inline]
Expand Down Expand Up @@ -129,8 +135,10 @@ mod tests {
));
let write_gen = WriteGenerator::create(ctx).await.unwrap();

let v = FuturesAsyncWriter::new(write_gen);
fn assert_writer_traits<T: std::fmt::Debug + Unpin + MaybeSend + Sync + 'static>(_: &T) {}

let v = FuturesAsyncWriter::new(write_gen);
assert_writer_traits(&v);
let _: Box<dyn Unpin + MaybeSend + Sync + 'static> = Box::new(v);
}
}
38 changes: 36 additions & 2 deletions core/core/src/types/write/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,32 @@ use crate::*;
/// creating writer with `append` enabled.
pub struct Writer {
/// Keep a reference to write context in writer.
_ctx: Arc<WriteContext>,
ctx: Arc<WriteContext>,
inner: WriteGenerator<oio::Writer>,
}

impl std::fmt::Debug for Writer {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let info = self.ctx.accessor().info();
let root = info.root();

f.debug_struct("Writer")
.field("scheme", &info.scheme())
.field("root", &root)
.field("path", &self.ctx.path())
.field("args", self.ctx.args())
.field("options", self.ctx.options())
.finish_non_exhaustive()
}
}

impl Writer {
/// Create a new writer from an `oio::Writer`.
pub(crate) async fn new(ctx: WriteContext) -> Result<Self> {
let ctx = Arc::new(ctx);
let inner = WriteGenerator::create(ctx.clone()).await?;

Ok(Self { _ctx: ctx, inner })
Ok(Self { ctx, inner })
}

/// Write [`Buffer`] into writer.
Expand Down Expand Up @@ -383,6 +398,7 @@ mod tests {
use rand::{Rng, RngExt};

use crate::Operator;
use crate::Result;
use crate::services;

fn gen_random_bytes() -> Vec<u8> {
Expand Down Expand Up @@ -452,4 +468,22 @@ mod tests {
chain_same.copy_to_bytes(chain_same.remaining())
);
}

#[tokio::test]
async fn test_debug() -> Result<()> {
let op = Operator::new(services::Memory::default()).unwrap().finish();
let path = "test_file";

let mut writer = op.writer_with(path).chunk(8).await?;
let output = format!("{writer:?}");
writer.abort().await?;

assert!(output.contains("Writer"), "{output}");
assert!(output.contains("memory"), "{output}");
assert!(output.contains(path), "{output}");
assert!(output.contains("args"), "{output}");
assert!(output.contains("options"), "{output}");

Ok(())
}
}
Loading