Skip to content

Commit

Permalink
Testify examples.
Browse files Browse the repository at this point in the history
Allow empty targets list in decl macro.
Update readme.
Fix `min_specialization`.
  • Loading branch information
raldone01 committed Sep 22, 2022
1 parent e0788ae commit 59b12c0
Show file tree
Hide file tree
Showing 14 changed files with 35 additions and 16 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ jobs:
- name: Build --all-features
run: cargo build --verbose --all-features --all-targets
- name: Run tests
run: cargo test --verbose
run: cargo test --verbose --lib --examples
- name: Run tests --all-features
run: cargo test --verbose --all-features
run: cargo test --verbose --all-features --lib --examples
1 change: 1 addition & 0 deletions trait_cast_rs/examples/manual.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ trait Cat: TraitcastableAny {
}
trait Mouse {}

#[cfg_attr(test, test)]
fn main() {
// The box is technically not needed but kept for added realism
let pet = Box::new(HybridPet {
Expand Down
1 change: 1 addition & 0 deletions trait_cast_rs/examples/manual_gen_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ impl<T: Display + 'static> TraitcastableAny for HybridPet<T> {
}
}

#[cfg_attr(test, test)]
fn main() {
// The box is technically not needed but kept for added realism
let pet = Box::new(HybridPet {
Expand Down
2 changes: 1 addition & 1 deletion trait_cast_rs/examples/with_decl_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ trait Dog {
trait Cat {
fn meow(&self);
}

#[cfg_attr(test, test)]
fn main() {
// The box is technically not needed but kept for added realism
let pet = Box::new(HybridPet {
Expand Down
2 changes: 1 addition & 1 deletion trait_cast_rs/examples/with_decl_macro_gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ trait Dog<T> {
trait Cat<A, B> {
fn meow(&self);
}

#[cfg_attr(test, test)]
fn main() {
// The box is technically not needed but kept for added realism
let pet = Box::new(HybridPet {
Expand Down
1 change: 1 addition & 0 deletions trait_cast_rs/examples/with_decl_macro_gen_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ make_trait_castable_decl! {
HybridPet<String> => (Dog, Cat<str>),
HybridPet<u8> => (Dog, Cat<u128>),
}
#[cfg_attr(test, test)]
fn main() {
// The box is technically not needed but kept for added realism
let pet = Box::new(HybridPet {
Expand Down
2 changes: 1 addition & 1 deletion trait_cast_rs/examples/with_proc_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ trait Dog {
trait Cat {
fn meow(&self);
}

#[cfg_attr(test, test)]
fn main() {
// The box is technically not needed but kept for added realism
let pet = Box::new(HybridPet {
Expand Down
2 changes: 1 addition & 1 deletion trait_cast_rs/examples/with_proc_macro_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ trait Dog {
trait Cat {
fn meow(&self);
}

#[cfg_attr(test, test)]
fn main() {
// The box is technically not needed but kept for added realism
let pet = Box::new(HybridPet::Name("Kokusnuss".to_string()));
Expand Down
2 changes: 1 addition & 1 deletion trait_cast_rs/examples/with_proc_macro_gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ trait Dog<T> {
trait Cat<A, B> {
fn meow(&self);
}

#[cfg_attr(test, test)]
fn main() {
// The box is technically not needed but kept for added realism
let pet = Box::new(HybridPet {
Expand Down
2 changes: 1 addition & 1 deletion trait_cast_rs/examples/with_proc_macro_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl Dog for HybridPet {
trait Dog {
fn rename(&mut self, new_tag: String);
}

#[cfg_attr(test, test)]
fn main() {
// The box is technically not needed but kept for added realism
let pet = Box::new(HybridPet {
Expand Down
14 changes: 10 additions & 4 deletions trait_cast_rs/src/decl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
/// ```
#[macro_export]
macro_rules! make_trait_castable_decl {
($($source:ty => ($($target:path),+ $(,)?)),+$(,)?) => {
($($source:ty => ($($target:path),* $(,)?)),+$(,)?) => {
$(
$(
impl $crate::TraitcastableTo<dyn $target> for $source {
Expand All @@ -26,15 +26,21 @@ macro_rules! make_trait_castable_decl {
casted.map(|selv| selv as &mut dyn $target)
}
}
)+
)*
impl $crate::TraitcastableAny for $source {
fn traitcast_targets(&self) -> &[$crate::TraitcastTarget] {
const TARGETS_LEN: usize = [$($crate::TraitcastTarget::from::<$source, dyn $target>(),)+].len();
const TARGETS_LEN: usize = {
let a:&[()] = &[$({
let _: &dyn $target;
()
},)*];
a.len()
};
const TARGETS: [$crate::TraitcastTarget; TARGETS_LEN] = {
let mut targets = [
$(
$crate::TraitcastTarget::from::<$source, dyn $target>(),
)+
)*
];
$crate::maybe_sort!(targets);
targets
Expand Down
1 change: 1 addition & 0 deletions trait_cast_rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ pub use trait_cast::*;

mod decl;

#[cfg(feature = "const_sort")]
#[doc(hidden)]
pub use const_sort_rs::ConstSliceSortExt;

Expand Down
9 changes: 7 additions & 2 deletions trait_cast_rs/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ pub use super::*;
mod test {
pub use super::*;

#[test]
fn test_wrong_source() {}
const fn _test_empty_trait_cast_targets() {
struct Woof {}

make_trait_castable_decl! {
Woof => (),
}
}
}
8 changes: 6 additions & 2 deletions trait_cast_rs/src/trait_cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,12 @@ impl<T: 'static> TraitcastableAny for T {
default fn traitcast_targets(&self) -> &[TraitcastTarget] {
&[]
}
default unsafe fn find_traitcast_target(&self, _target: TypeId) -> Option<&TraitcastTarget> {
None
default unsafe fn find_traitcast_target(&self, target: TypeId) -> Option<&TraitcastTarget> {
// Note: copied from above
self
.traitcast_targets()
.iter()
.find(|possible| possible.target_type_id == target)
}
}
impl Debug for dyn TraitcastableAny {
Expand Down

0 comments on commit 59b12c0

Please sign in to comment.