Skip to content

Commit

Permalink
Attempt to remove YokeTraitHack
Browse files Browse the repository at this point in the history
  • Loading branch information
sffc committed Oct 1, 2021
1 parent 530279a commit b316197
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 32 deletions.
28 changes: 12 additions & 16 deletions provider/core/src/data_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use crate::error::Error;
use crate::marker::DataMarker;
use crate::resource::ResourceKey;
use crate::resource::ResourcePath;
use crate::yoke::trait_hack::YokeTraitHack;
use crate::yoke::*;

use alloc::rc::Rc;
Expand Down Expand Up @@ -184,7 +183,7 @@ where
impl<'data, M> Clone for DataPayload<'data, M>
where
M: DataMarker<'data>,
for<'a> YokeTraitHack<<M::Yokeable as Yokeable<'a>>::Output>: Clone,
for<'a> <M::Yokeable as Yokeable<'a>>::Output: Clone,
{
fn clone(&self) -> Self {
use DataPayloadInner::*;
Expand All @@ -200,24 +199,24 @@ where
impl<'data, M> PartialEq for DataPayload<'data, M>
where
M: DataMarker<'data>,
for<'a> YokeTraitHack<<M::Yokeable as Yokeable<'a>>::Output>: PartialEq,
for<'a> <M::Yokeable as Yokeable<'a>>::Output: PartialEq,
{
fn eq(&self, other: &Self) -> bool {
YokeTraitHack(self.get()).into_ref() == YokeTraitHack(other.get()).into_ref()
self.get() == other.get()
}
}

impl<'data, M> Eq for DataPayload<'data, M>
where
M: DataMarker<'data>,
for<'a> YokeTraitHack<<M::Yokeable as Yokeable<'a>>::Output>: Eq,
for<'a> <M::Yokeable as Yokeable<'a>>::Output: Eq,
{
}

#[test]
fn test_clone_eq() {
use crate::marker::CowStrMarker;
let p1 = DataPayload::<CowStrMarker>::from_static_str("Demo");
use crate::hello_world::HelloWorldV1Marker;
let p1 = DataPayload::<HelloWorldV1Marker>::from_owned(Default::default());
let p2 = p1.clone();
assert_eq!(p1, p2);
}
Expand Down Expand Up @@ -481,14 +480,14 @@ where
/// - [`DataPayload::map_project_with_capture()`] to pass context to the mapping function
/// - [`DataPayload::map_project_cloned_with_capture()`] to do both of these things
///
/// All `DataPayload::map_project()` functions require Rust 1.56 or newer; for details, see
/// (#1061)[https://github.com/unicode-org/icu4x/issues/1061].
///
/// # Examples
///
/// Map from `HelloWorldV1` to a `Cow<str>` containing just the message:
///
/// ***[#1061](https://github.com/unicode-org/icu4x/issues/1061): The following example
/// requires Rust 1.56.***
///
/// ```ignore
/// ```
/// use icu_provider::hello_world::*;
/// use icu_provider::prelude::*;
/// use std::borrow::Cow;
Expand Down Expand Up @@ -637,10 +636,7 @@ where
///
/// Prior to Rust 1.57, pass the capture by value instead of by reference:
///
/// ***[#1061](https://github.com/unicode-org/icu4x/issues/1061): The following example
/// requires Rust 1.56.***
///
/// ```ignore
/// ```
/// // Same imports and definitions as above
/// # use icu_provider::hello_world::*;
/// # use icu_provider::prelude::*;
Expand Down Expand Up @@ -823,7 +819,7 @@ where
impl<'data, M> Clone for DataResponse<'data, M>
where
M: DataMarker<'data>,
for<'a> YokeTraitHack<<M::Yokeable as Yokeable<'a>>::Output>: Clone,
for<'a> <M::Yokeable as Yokeable<'a>>::Output: Clone,
{
fn clone(&self) -> Self {
Self {
Expand Down
30 changes: 28 additions & 2 deletions provider/core/src/struct_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use crate::error::Error;
use crate::prelude::*;
use crate::yoke::trait_hack::YokeTraitHack;
use crate::yoke::*;

/// A data provider that returns clones of a constant data payload.
Expand Down Expand Up @@ -49,7 +48,7 @@ where
impl<'data, M> DataProvider<'data, M> for StructProvider<'data, M>
where
M: DataMarker<'data>,
for<'a> YokeTraitHack<<M::Yokeable as Yokeable<'a>>::Output>: Clone,
for<'a> <M::Yokeable as Yokeable<'a>>::Output: Clone,
{
fn load_payload(&self, req: &DataRequest) -> Result<DataResponse<'data, M>, Error> {
req.resource_path.key.match_key(self.key)?;
Expand All @@ -59,3 +58,30 @@ where
})
}
}

#[test]
fn test_traits() {
#[derive(Clone, yoke::Yokeable)]
struct SimpleStruct(pub u32);

impl<'data> DataMarker<'data> for SimpleStruct {
type Yokeable = SimpleStruct;
type Cart = SimpleStruct;
}

// A placeholder key to use to serve the data struct
const SAMPLE_KEY: ResourceKey = crate::resource_key!(x, "xyz", "example", 1);

let provider = StructProvider {
key: SAMPLE_KEY,
data: DataPayload::from_owned(SimpleStruct(42)),
};

let payload: DataPayload<SimpleStruct> = provider
.load_payload(&DataRequest::from(SAMPLE_KEY))
.expect("Load should succeed")
.take_payload()
.expect("Data should be present");

assert_eq!(payload.get().0, 42);
}
20 changes: 6 additions & 14 deletions utils/yoke/src/yoke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

use crate::trait_hack::YokeTraitHack;
use crate::IsCovariant;
use crate::Yokeable;
use core::marker::PhantomData;
Expand Down Expand Up @@ -484,14 +483,11 @@ unsafe impl CloneableCart for () {}
/// (e.g., from `.with_mut()`), that data will need to be cloned.
impl<Y: for<'a> Yokeable<'a>, C: CloneableCart> Clone for Yoke<Y, C>
where
for<'a> YokeTraitHack<<Y as Yokeable<'a>>::Output>: Clone,
for<'a> <Y as Yokeable<'a>>::Output: Clone,
{
fn clone(&self) -> Self {
let this: &Y::Output = self.get();
// We have an &T not a T, and we can clone YokeTraitHack<T>
let this_hack = YokeTraitHack(this).into_ref();
Yoke {
yokeable: unsafe { Y::make(this_hack.clone().0) },
yokeable: unsafe { Y::make(self.get().clone()) },
cart: self.cart.clone(),
}
}
Expand Down Expand Up @@ -546,13 +542,12 @@ impl<Y: for<'a> Yokeable<'a>, C> Yoke<Y, C> {
/// this from taking a capturing closure, however [`Yoke::project_with_capture()`]
/// can be used for the same use cases.
///
/// This function requires Rust 1.56 or newer; for details, see
/// (#1061)[https://github.com/unicode-org/icu4x/issues/1061].
///
/// This can be used, for example, to transform data from one format to another:
///
/// ***[#1061](https://github.com/unicode-org/icu4x/issues/1061): The following example
/// requires Rust 1.56.***
///
/// ```rust,ignore
/// ```rust
/// # use std::rc::Rc;
/// # use yoke::Yoke;
/// #
Expand All @@ -564,10 +559,7 @@ impl<Y: for<'a> Yokeable<'a>, C> Yoke<Y, C> {
///
/// This can also be used to create a yoke for a subfield
///
/// ***[#1061](https://github.com/unicode-org/icu4x/issues/1061): The following example
/// requires Rust 1.56.***
///
/// ```rust,ignore
/// ```rust
/// # use std::borrow::Cow;
/// # use yoke::{Yoke, Yokeable};
/// # use std::mem;
Expand Down

0 comments on commit b316197

Please sign in to comment.