Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Any/BufferProvider for some smart pointers #4255

Merged
merged 2 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
19 changes: 19 additions & 0 deletions provider/core/src/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,12 +350,31 @@ pub trait AnyProvider {
fn load_any(&self, key: DataKey, req: DataRequest) -> Result<AnyResponse, DataError>;
}

impl<'a, T: AnyProvider + ?Sized> AnyProvider for &'a T {
fn load_any(&self, key: DataKey, req: DataRequest) -> Result<AnyResponse, DataError> {
(**self).load_any(key, req)
}
}

impl<T: AnyProvider + ?Sized> AnyProvider for alloc::boxed::Box<T> {
fn load_any(&self, key: DataKey, req: DataRequest) -> Result<AnyResponse, DataError> {
(**self).load_any(key, req)
}
}

impl<T: AnyProvider + ?Sized> AnyProvider for alloc::rc::Rc<T> {
fn load_any(&self, key: DataKey, req: DataRequest) -> Result<AnyResponse, DataError> {
(**self).load_any(key, req)
}
}

#[cfg(target_has_atomic = "ptr")]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thought: hm, in general we haven't been careful about this, but I guess it doesn't hurt to start?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think in other places we've been using #[cfg(feature = "std")] for gating Arc. I'm not sure which of these is better though. I think the main thing is to not mess up the compilation of targets like wasm32-unknown-unknown. If this works without needing the std feature then that's great and we should use this pattern in more places.

impl<T: AnyProvider + ?Sized> AnyProvider for alloc::sync::Arc<T> {
fn load_any(&self, key: DataKey, req: DataRequest) -> Result<AnyResponse, DataError> {
(**self).load_any(key, req)
}
}

/// A wrapper over `DynamicDataProvider<AnyMarker>` that implements `AnyProvider`
#[allow(clippy::exhaustive_structs)] // newtype
#[derive(Debug)]
Expand Down
31 changes: 31 additions & 0 deletions provider/core/src/buf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,16 @@ pub trait BufferProvider {
) -> Result<DataResponse<BufferMarker>, DataError>;
}

impl<'a, T: BufferProvider + ?Sized> BufferProvider for &'a T {
fn load_buffer(
&self,
key: DataKey,
req: DataRequest,
) -> Result<DataResponse<BufferMarker>, DataError> {
(**self).load_buffer(key, req)
}
}

impl<T: BufferProvider + ?Sized> BufferProvider for alloc::boxed::Box<T> {
fn load_buffer(
&self,
Expand All @@ -103,6 +113,27 @@ impl<T: BufferProvider + ?Sized> BufferProvider for alloc::boxed::Box<T> {
}
}

impl<T: BufferProvider + ?Sized> BufferProvider for alloc::rc::Rc<T> {
fn load_buffer(
&self,
key: DataKey,
req: DataRequest,
) -> Result<DataResponse<BufferMarker>, DataError> {
(**self).load_buffer(key, req)
}
}

#[cfg(target_has_atomic = "ptr")]
impl<T: BufferProvider + ?Sized> BufferProvider for alloc::sync::Arc<T> {
fn load_buffer(
&self,
key: DataKey,
req: DataRequest,
) -> Result<DataResponse<BufferMarker>, DataError> {
(**self).load_buffer(key, req)
}
}

/// An enum expressing all Serde formats known to ICU4X.
#[derive(Debug, PartialEq, Eq, Hash, Copy, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
Expand Down