-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement blanket implementation for
Cow
wrapper
- Loading branch information
Showing
12 changed files
with
322 additions
and
15 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
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,109 @@ | ||
use syn::parse_quote; | ||
|
||
use crate::derive::Receiver; | ||
use crate::derive::WrapperType; | ||
|
||
struct CowType; | ||
|
||
impl WrapperType for CowType { | ||
const NAME: &'static str = "Cow"; | ||
const RECEIVERS: &'static [Receiver] = &[Receiver::Ref]; | ||
const BOUNDS: &'static [&'static str] = &["ToOwned"]; | ||
fn wrap(ty: &syn::Ident) -> syn::Type { | ||
parse_quote!(std::borrow::Cow<'_, #ty>) | ||
} | ||
} | ||
|
||
pub fn derive(trait_: &syn::ItemTrait) -> syn::Result<syn::ItemImpl> { | ||
CowType::derive(trait_) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
mod derive { | ||
|
||
use syn::parse_quote; | ||
|
||
#[test] | ||
fn empty() { | ||
let trait_ = parse_quote!( | ||
trait MyTrait {} | ||
); | ||
let derived = super::super::derive(&trait_).unwrap(); | ||
assert_eq!( | ||
derived, | ||
parse_quote!( | ||
#[automatically_derived] | ||
impl<MT: MyTrait + ?Sized + ToOwned> MyTrait for std::borrow::Cow<'_, MT> {} | ||
) | ||
); | ||
} | ||
|
||
#[test] | ||
fn receiver_ref() { | ||
let trait_ = parse_quote!( | ||
trait Trait { | ||
fn my_method(&self); | ||
} | ||
); | ||
assert_eq!( | ||
super::super::derive(&trait_).unwrap(), | ||
parse_quote!( | ||
#[automatically_derived] | ||
impl<T: Trait + ?Sized + ToOwned> Trait for std::borrow::Cow<'_, T> { | ||
#[inline] | ||
fn my_method(&self) { | ||
(*(*self)).my_method() | ||
} | ||
} | ||
) | ||
); | ||
} | ||
|
||
#[test] | ||
fn receiver_mut() { | ||
let trait_ = parse_quote!( | ||
trait Trait { | ||
fn my_method(&mut self); | ||
} | ||
); | ||
assert!(super::super::derive(&trait_).is_err()); | ||
} | ||
|
||
#[test] | ||
fn receiver_self() { | ||
let trait_ = parse_quote!( | ||
trait MyTrait { | ||
fn my_method(self); | ||
} | ||
); | ||
assert!(super::super::derive(&trait_).is_err()); | ||
} | ||
|
||
#[test] | ||
fn receiver_arbitrary() { | ||
let trait_ = parse_quote!( | ||
trait Trait { | ||
fn my_method(self: Box<Self>); | ||
} | ||
); | ||
assert!(super::super::derive(&trait_).is_err()); | ||
} | ||
|
||
#[test] | ||
fn generics() { | ||
let trait_ = parse_quote!( | ||
trait MyTrait<T> {} | ||
); | ||
let derived = super::super::derive(&trait_).unwrap(); | ||
|
||
assert_eq!( | ||
derived, | ||
parse_quote!( | ||
#[automatically_derived] | ||
impl<T, MT: MyTrait<T> + ?Sized + ToOwned> MyTrait<T> for std::borrow::Cow<'_, MT> {} | ||
) | ||
); | ||
} | ||
} | ||
} |
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,7 @@ | ||
extern crate trybuild; | ||
|
||
fn main() { | ||
let t = trybuild::TestCases::new(); | ||
t.compile_fail(file!().replace("mod.rs", "fails/*.rs")); | ||
t.pass(file!().replace("mod.rs", "successes/*.rs")); | ||
} |
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,21 @@ | ||
use blanket::blanket; | ||
use impls::impls; | ||
|
||
use std::borrow::Cow; | ||
|
||
#[blanket(derive(Cow))] | ||
pub trait StaticChecker { | ||
fn check(); | ||
} | ||
|
||
#[derive(Default, Clone)] | ||
struct NoOpChecker; | ||
|
||
impl StaticChecker for NoOpChecker { | ||
fn check() {} | ||
} | ||
|
||
fn main() { | ||
assert!(impls!( NoOpChecker: StaticChecker)); | ||
assert!(impls!(Cow<NoOpChecker>: StaticChecker)); | ||
} |
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,21 @@ | ||
use blanket::blanket; | ||
use impls::impls; | ||
|
||
use std::borrow::Cow; | ||
|
||
#[blanket(derive(Cow))] | ||
pub trait StaticChecker { | ||
fn check() -> Result<(), String>; | ||
} | ||
|
||
#[derive(Default, Clone)] | ||
struct NoOpChecker; | ||
|
||
impl StaticChecker for NoOpChecker { | ||
fn check() -> Result<(), String> { Ok(()) } | ||
} | ||
|
||
fn main() { | ||
assert!(impls!( NoOpChecker: StaticChecker)); | ||
assert!(impls!(Cow<NoOpChecker>: StaticChecker)); | ||
} |
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,33 @@ | ||
use std::borrow::Cow; | ||
use std::sync::Arc; | ||
use std::sync::RwLock; | ||
|
||
use blanket::blanket; | ||
use impls::impls; | ||
|
||
#[blanket(derive(Cow))] | ||
pub trait Counter { | ||
type Return: Clone; // <- verify this | ||
fn increment(&self) -> Self::Return; | ||
} | ||
|
||
#[derive(Default, Clone)] | ||
struct AtomicCounter { | ||
count: Arc<RwLock<u8>>, | ||
} | ||
|
||
impl Counter for AtomicCounter { | ||
// Generate something like `type Return = <A as Assoc>::Return;`. | ||
type Return = u8; | ||
fn increment(&self) -> u8 { | ||
let mut guard = self.count.try_write().unwrap(); | ||
let out = *guard; | ||
*guard += 1; | ||
out | ||
} | ||
} | ||
|
||
fn main() { | ||
assert!(impls!(AtomicCounter: Counter)); | ||
assert!(impls!(Cow<AtomicCounter>: Counter)); | ||
} |
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,24 @@ | ||
extern crate blanket; | ||
extern crate impls; | ||
|
||
use std::borrow::Cow; | ||
|
||
use blanket::blanket; | ||
use impls::impls; | ||
|
||
#[blanket(derive(Cow))] | ||
pub trait Counter { | ||
fn count(&self); | ||
} | ||
|
||
#[derive(Default, Clone)] | ||
struct AtomicCounter {} | ||
|
||
impl Counter for AtomicCounter { | ||
fn count(&self) {} | ||
} | ||
|
||
fn main() { | ||
assert!(impls!(AtomicCounter: Counter)); | ||
assert!(impls!(Cow<AtomicCounter>: Counter)); | ||
} |
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,30 @@ | ||
extern crate blanket; | ||
extern crate impls; | ||
|
||
use std::borrow::Cow; | ||
|
||
use blanket::blanket; | ||
use impls::impls; | ||
|
||
#[blanket(derive(Cow))] | ||
pub trait AsRef2<T> { | ||
fn as_ref2(&self) -> &T; | ||
} | ||
|
||
#[derive(Default, Clone)] | ||
struct Owner<T> { | ||
owned: T, | ||
} | ||
|
||
impl<T> AsRef2<T> for Owner<T> { | ||
fn as_ref2(&self) -> &T { | ||
&self.owned | ||
} | ||
} | ||
|
||
fn main() { | ||
assert!(impls!(Owner<String>: AsRef2<String>)); | ||
assert!(impls!(Cow<Owner<String>>: AsRef2<String>)); | ||
assert!(impls!(Owner<bool>: AsRef2<bool>)); | ||
assert!(impls!(Cow<Owner<bool>>: AsRef2<bool>)); | ||
} |
Oops, something went wrong.