fkoep / downcast-rs Goto Github PK
View Code? Open in Web Editor NEWTrait for downcasting trait objects back to their original types.
License: MIT License
Trait for downcasting trait objects back to their original types.
License: MIT License
I'm referring to https://github.com/fkoep/downcast-rs/blob/master/src/lib.rs#L107-L117
One thing that seems scary, is it's relying on the internal layout of https://doc.rust-lang.org/std/raw/struct.TraitObject.html . This is nightly specifically because it's not stable (See rust-lang/rust#27751 ) , so if the layout were to change for whatever reason (However unlikely) it would probably break this horribly (And what's more, it would probably work fine on some compiler versions but not others, and possibly not break with just a guaranteed panic but with memory-unsafety/UB).
The assert assert_eq!(mem::size_of::<&T>(), mem::size_of::<TraitObject>());
doesn't feel sufficient to me to guarantee that what is being done is safe. If I recall correctly, slices references(Which satisfy the :?Sized check) have the same size but mean a completely different thing.
Nemo157 on the community discord's #black-magic channel pointed out TraitObject
also must be repr(C) for there to be any layout guarantees about it (Reordering, etc).
The latest clippy (rustc nightly 1.41.0) warns that downcast's generated methods lack a # Safety
section in their docs. The appropriate course of action would probably be to silence the warning with #![allow(clippy::missing_safety_doc)]
.
The build is broken on the latest Rust nightly
> rustup run nightly rustc --version
rustc 1.33.0-nightly (bf669d1e3 2019-01-25)
> cargo +nightly test
Compiling downcast v0.9.2 (/usr/home/somers/src/rust/downcast-rs)
error[E0034]: multiple applicable items in scope
--> src/lib.rs:120:38
|
120 | fn is_type(&self) -> bool { self.type_id() == TypeId::of::<T>() }
| ^^^^^^^ multiple `type_id` found
|
note: candidate #1 is defined in the trait `Any`
--> src/lib.rs:29:5
|
29 | fn type_id(&self) -> TypeId { TypeId::of::<Self>() }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
= help: to disambiguate the method call, write `Any::type_id(&self)` instead
note: candidate #2 is defined in the trait `std::any::Any`
= help: to disambiguate the method call, write `std::any::Any::type_id(&self)` instead
error[E0034]: multiple applicable items in scope
--> src/lib.rs:120:38
|
120 | fn is_type(&self) -> bool { self.type_id() == TypeId::of::<T>() }
| ^^^^^^^ multiple `type_id` found
|
note: candidate #1 is defined in the trait `Any`
--> src/lib.rs:29:5
|
29 | fn type_id(&self) -> TypeId { TypeId::of::<Self>() }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
= help: to disambiguate the method call, write `Any::type_id(&self)` instead
note: candidate #2 is defined in the trait `std::any::Any`
= help: to disambiguate the method call, write `std::any::Any::type_id(&self)` instead
error: aborting due to previous error
For more information about this error, try `rustc --explain E0034`.
error: aborting due to previous error
For more information about this error, try `rustc --explain E0034`.
error: Could not compile `downcast`.
warning: build failed, waiting for other jobs to finish...
error: Could not compile `downcast`.
To learn more, run the command again with --verbose.
Would it be possible to make another release of this crate? I'm especially interested in the #[allow(missing_docs)]
fix. And for reasons I don't fully understand, downcast works with my crate, but downcast-rs does not.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.