Skip to content

Commit

Permalink
feat: Add assert for public types to ensure Send + Sync (#2237)
Browse files Browse the repository at this point in the history
feat: Add assert for public types to make sure Send + Sync

Signed-off-by: Xuanwo <github@xuanwo.io>
  • Loading branch information
Xuanwo authored May 8, 2023
1 parent 92e0d0a commit 7ce2371
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 1 deletion.
14 changes: 14 additions & 0 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,18 @@ mod tests {
assert_eq!(1, size_of::<EntryMode>());
assert_eq!(24, size_of::<Scheme>());
}

/// This is used to make sure our public API implement Send + Sync
trait AssertSendSync: Send + Sync {}
impl AssertSendSync for Entry {}
impl AssertSendSync for Capability {}
impl AssertSendSync for Error {}
impl AssertSendSync for Reader {}
impl AssertSendSync for Writer {}
impl AssertSendSync for Lister {}
impl AssertSendSync for Operator {}
impl AssertSendSync for BlockingReader {}
impl AssertSendSync for BlockingWriter {}
impl AssertSendSync for BlockingLister {}
impl AssertSendSync for BlockingOperator {}
}
2 changes: 1 addition & 1 deletion core/src/raw/oio/page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ impl<P: Page> Page for Option<P> {
}

/// BlockingPage is the blocking version of [`Page`].
pub trait BlockingPage: 'static {
pub trait BlockingPage: Send + 'static {
/// Fetch a new page of [`Entry`]
///
/// `Ok(None)` means all pages have been returned. Any following call
Expand Down
10 changes: 10 additions & 0 deletions core/src/types/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ pub struct Lister {
fut: Option<BoxFuture<'static, (oio::Pager, Result<Option<Vec<oio::Entry>>>)>>,
}

/// # Safety
///
/// Lister will only be accessed by `&mut Self`
unsafe impl Sync for Lister {}

impl Lister {
/// Create a new lister.
pub(crate) fn new(pager: oio::Pager) -> Self {
Expand Down Expand Up @@ -166,6 +171,11 @@ pub struct BlockingLister {
buf: VecDeque<oio::Entry>,
}

/// # Safety
///
/// BlockingLister will only be accessed by `&mut Self`
unsafe impl Sync for BlockingLister {}

impl BlockingLister {
/// Create a new lister.
pub(crate) fn new(pager: oio::BlockingPager) -> Self {
Expand Down
5 changes: 5 additions & 0 deletions core/src/types/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ pub struct Writer {
state: State,
}

/// # Safety
///
/// Writer will only be accessed by `&mut Self`
unsafe impl Sync for Writer {}

impl Writer {
/// Create a new writer.
///
Expand Down

0 comments on commit 7ce2371

Please sign in to comment.