Skip to content

Commit

Permalink
Fix the doctest in the README.md.
Browse files Browse the repository at this point in the history
See rust-lang/rust#134221 for more details.
  • Loading branch information
raldone01 committed Dec 12, 2024
1 parent ca8e811 commit dffdade
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
6 changes: 2 additions & 4 deletions trait_cast_rs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ Note: No modifications on the *target* traits are necessary. Which allows you to
## Example

```rust
#![feature(
ptr_metadata, //
)]
#![feature(ptr_metadata, min_specialization)]
use trait_cast_rs::{
make_trait_castable, TraitcastableAny, TraitcastableAnyInfra, TraitcastableAnyInfraExt,
};
Expand All @@ -52,7 +50,7 @@ trait Print {
}
impl Print for Source {
fn print(&self) {
println!("{}", self.0)
println!("{}", self.0);
}
}

Expand Down
26 changes: 25 additions & 1 deletion trait_cast_rs/src/test.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::make_trait_castable_decl;
use crate::{make_trait_castable_decl, TraitcastableAny, TraitcastableAnyInfra};
use alloc::boxed::Box;
use trait_cast_impl_rs::make_trait_castable;

const fn _test_empty_trait_cast_targets() {
struct Woof {}
Expand All @@ -7,3 +9,25 @@ const fn _test_empty_trait_cast_targets() {
Woof => (),
}
}

make_trait_castable_decl! {
Source => (Print)
}

struct Source(i32);
trait Print {
fn print(&self) -> i32;
}
impl Print for Source {
fn print(&self) -> i32 {
self.0
}
}

#[test]
fn test_trait_castable() {
let source = Box::new(Source(5));
let castable: Box<dyn TraitcastableAny> = source;
let x: &dyn Print = castable.downcast_ref().unwrap();
x.print();
}
2 changes: 1 addition & 1 deletion trait_cast_rs/src/trait_cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ pub trait TraitcastableAnyInfraExt<Target: ?Sized + 'static>: Sized {
#[cfg(feature = "min_specialization")]
// Safety:
// Since `traitcast_targets` returns nothing this is always safe.
// `find_traitcast_target` has the default implementations
// `find_traitcast_target` has the default implementations.
unsafe impl<T: 'static> TraitcastableAny for T {
default fn traitcast_targets(&self) -> &[TraitcastTarget] {
&[]
Expand Down

0 comments on commit dffdade

Please sign in to comment.