chris-morgan / anymap Goto Github PK
View Code? Open in Web Editor NEWA safe and convenient store for one value of each type
License: Other
A safe and convenient store for one value of each type
License: Other
I want to crate a Map<Component>
, but whatever I do, Rust always says error[E0277]: the trait bound `Component + 'static: anymap::any::UncheckedAnyExt` is not satisfied
.
I have tried so much now:
pub trait Component {
pub trait Component: std::any::Any {
pub trait Component: anymap::any::Any {
pub trait Component: anymap::any::Any + anymap::any::UncheckedAnyExt {
pub trait Component: 'static + anymap::any::Any + anymap::any::UncheckedAnyExt {
pub trait Component: 'static + std::any::Any + anymap::any::Any + anymap::any::UncheckedAnyExt {
I am lost on what to do...
Just for the record, since we already discussed this a bit.
It would be nice if we created a concurrent
feature that would basically add the Sync + Send
bounds where necessary. I already do something similar in my minor fork and found that it's a very straightforward change compared to making it clonable.
Personally I really need this so that I could put stuff in an Arc
and send it to a thread pool, but I understand if it's more of a hassle than it's worth here.
Specifically I need it to be both clonable and concurrent. What I was discussing on IRC was that I don't know if it'd be possible to have to independent yet composable features for each thing: clone
and concurrent
, given the fact (?) that the with_clone
stuff would need to have the Sync + Send
bounds added.
I see a number of commits here, but on crates.io the library is Last Updated: 3 years ago
.
What is maintainership status of the library?
Howdy,
The size of TypeId changed in rust-lang/rust@9e5573a, and the unittest type_id_hasher
no longer builds:
cannot transmute between types of different sizes, or dependently-sized types
|
638 | assert_eq!(hasher.finish(), unsafe { core::mem::transmute::<TypeId, u64>(type_id) });
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: source type: `TypeId` (128 bits)
= note: target type: `u64` (64 bits)
It seems to me that the commit doesn't affect the behavior of the anymap crate, it only affects the unittest. Could you please confirm that that is the case?
for example lazy_static? or using ONCE?
Best I could do is
pub fn model_store() -> Arc<Mutex<AnyMap>> {
static mut STORE: *const Arc<Mutex<anymap::AnyMap>> = 0 as *const Arc<Mutex<anymap::AnyMap>>;
static ONCE: Once = Once::new();
ONCE.call_once(|| {
let singleton = AnyMap::new();
unsafe { STORE = std::mem::transmute(Arc::new(Mutex::new(singleton)))};
});
unsafe { (*STORE).clone() } // Is this safe?
}
which is definitely unsafe?
anymap::Entry::or_insert
and anymap::Entry::or_insert_with
require a Clone
bound on the element to be inserted.
It is unclear from both the docs and the source why that is the case, or if its intentional.
Hi, @chris-morgan could you or somebody else involved in this project please publish a new release on crates.io, or possibly hand over maintainership of this project to somebody that can publish new releases?
Looks like anymap could use a release so people can pick up the fix for #26, which is not in 0.12.
Could the AnyMap implement Clone ?
TypeIdHasher is useful for other abstractions dealing with dynamic typing.
I have copy-pasted that code into a private project of mine, because I wanted to use it to implement another collection type that is similar to AnyMap.
The collection I implemented seems generally-useful. It might be a good idea for me to publish it as a crate of my own (I don't think it belongs in anymap, as it is different enough from the purpose of this crate). However, to do that, I would need to duplicate the TypeIdHasher code from anymap. If we split that off into a separate crate, we could both depend on it instead of duplicating code.
Rustc throws the following error:
warning: the trait `anymap::any::CloneToAny` cannot be made into an object
|
= note: `#[warn(where_clauses_object_safety)]` on by default
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #51443 <https://github.com/rust-lang/rust/issues/51443>
= note: method `clone_to_any_send` references the `Self` type in where clauses
warning: the trait `anymap::any::CloneToAny` cannot be made into an object
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #51443 <https://github.com/rust-lang/rust/issues/51443>
= note: method `clone_to_any_sync` references the `Self` type in where clauses
warning: the trait `anymap::any::CloneToAny` cannot be made into an object
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #51443 <https://github.com/rust-lang/rust/issues/51443>
= note: method `clone_to_any_send_sync` references the `Self` type in where clauses
warning: the trait `any::CloneToAny` cannot be made into an object
--> /mnt/src/git/anymap/src/any.rs:15:8
|
15 | fn clone_to_any_send(&self) -> Box<CloneAny + Send> where Self: Send;
| ^^^^^^^^^^^^^^^^^
|
= note: `#[warn(where_clauses_object_safety)]` on by default
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #51443 <https://github.com/rust-lang/rust/issues/51443>
= note: method `clone_to_any_send` references the `Self` type in where clauses
Does anymap
rely on a trick deemed unsound?
This is not a bug report, but a request for comments on a possibly new feature.
AnyMap internally stores data as boxed references to Any. Since Any does not carry Share and Send traits, the compiler does not allow access to the map from multiple tasks even though the original types of stored data should in principle allow it.
I have modified AnyMap by adding Send and Share bounds to Any and to type parameters of generic functions. The result is ConcurrentAnyMap, tivek/anymap@63f992f. It is safely accessible from more than just the creating task through immutable references, which makes it useful as a centralized, immutable data store. std::sync::Arc so far seems to be happy.
At this point I do not like the code duplication between AnyMap and ConcurrentAnyMap. Share+Send need to be added at several places, which is pretty intrusive. To me it seems deriving would not work (but, I have just started with Rust and maybe I'm missing something obvious).
Hope someone finds it useful, and I'd appreciate your comments.
Dear @chris-morgan,
I don't wish to be rude, but from the issue tracker e.g. #45 I wonder if you have had enough of maintaining this crate / real life has gotten in the way or whatever.
I have released a new version under the crate name anymap3
but I wonder whether you'd prefer for someone to look after the original crate / 'hand over' maintainership?
I noticed some users of the crate have moved to vendoring their own copy with the soundness issues fixed (and other crates have rolled their own internal ones), which overall seems bad for 'the community' as I can't just pull in their copy into my project ;-).
It's already a fairly 'complete' and maintenance-mode crate so if the idea interested you, I'd be happy to take over its keepership.
Thanks for writing this crate for us and saving us having to do it ourselves.
anymap is still on the list of advisory because version 1.0.0 is not released yet.
rustsec/advisory-db#1281
We should work towards getting this to build on the "stable" releases, e.g. beta. This means we have to remove our dependence on the std_misc
and core
features.
core
seems to be needed for std::raw::TraitObject
which is needed for downcasting functionality.std_misc
seems to be needed for std::collections::hash_state::HashState
and Drain
Perhaps it might be worth a look at @reem's typemap which doesn't require any features. In the worst case, we can probably salvage those features behind some sort of "nightly"
feature, or better yet each one of those features under a separate feature, like "downcast"
and "drain"
.
Compiling anymap on the current nightly results in this warning: rust-lang/rust#51443
warning: the trait `any::CloneToAny` cannot be made into an object
--> src/any.rs:15:5
|
15 | fn clone_to_any_send(&self) -> Box<CloneAny + Send> where Self: Send;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: #[warn(where_clauses_object_safety)] on by default
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #51443 <https://github.com/rust-lang/rust/issues/51443>
= note: method `clone_to_any_send` references the `Self` type in where clauses
(same warning for all clone_to_any_X
methods)
I'm trying to write an entity component system for game development, and I've gotten to the point where I would like to start caching lists of components for a system, so my first thought was to have a component trait that returns a list of TypeIds which describes which component types are required. Then the idea is to iterate over them when it is time to update the list of components and retrieve the value of the AnyMap by the TypeId. Currently there doesn't seem to be a way to do that exposed in your library, but it doesn't look like it would be too hard, but I'm not confident in my Rust skills yet to make a pull request myself.
My request is for a set of methods like contains_typeid, get_by_typeid, etc...
Is there any way to have AnyMapEq/AnyMapPartialEq or something?
Hi, I am scanning the anymap in the latest version with my own static analyzer tool.
Unsafe conversion found at: src/any.rs
#[inline]
unsafe fn downcast_ref_unchecked<T: 'static>(&self) -> &T {
&*(self as *const Self as *const T)
}
#[inline]
unsafe fn downcast_mut_unchecked<T: 'static>(&mut self) -> &mut T {
&mut *(self as *mut Self as *mut T)
}
#[inline]
unsafe fn downcast_unchecked<T: 'static>(self: Box<Self>) -> Box<T> {
Box::from_raw(Box::into_raw(self) as *mut T)
}
This unsound implementation would create a misalignment issues, if the generics type T
is not properly handled like it's other random types.
This would potentially cause undefined behaviors in Rust. If we further manipulate the problematic converted types, it would potentially lead to different consequences. I am reporting this issue for your attention.
jhaddad@jhaddad-rmbp15 ~/dev/anymap$ cargo test master
Compiling anymap v0.10.3 (file:///Users/jhaddad/dev/anymap)
src/lib.rs:4:19: 4:33 error: unstable feature
src/lib.rs:4 #![cfg_attr(test, feature(test))]
^~~~~~~~~~~~~~
note: this feature may not be used in the stable release channel
error: aborting due to previous error
Could not compile `anymap`.
I was using default to initialize some struct members as well as using #[derive(Default)]
but anymap
doesn't support Default
.
In 1.0.0-beta.2
, the any
module is now private and only CloneAny
is exported, meaning the Downcast
trait is no longer visible.
building anymap on nightly rust now triggers a future incompatibility warning, here's a copypasted terminal output
The package `anymap v1.0.0-beta.2` currently triggers the following future incompatibility lints:
> warning: adding an auto trait `Send` to a trait object in a pointer cast may cause UB later on
> --> /home/draconium/.cargo/registry/src/index.crates.io-6f17d22bba15001f/anymap-1.0.0-beta.2/src/any.rs:37:40
> |
> 37 | unsafe { Box::from_raw(raw as *mut $t) }
> | ^^^^^^^^^^^^^^
> ...
> 144 | impl_clone!(dyn CloneAny + Send);
> | -------------------------------- in this macro invocation
> |
> = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
> = note: for more information, see issue #127323 <https://github.com/rust-lang/rust/issues/127323>
> = note: this warning originates in the macro `impl_clone` (in Nightly builds, run with -Z macro-backtrace for more info)
>
> warning: adding auto traits `Send` and `Sync` to a trait object in a pointer cast may cause UB later on
> --> /home/draconium/.cargo/registry/src/index.crates.io-6f17d22bba15001f/anymap-1.0.0-beta.2/src/any.rs:37:40
> |
> 37 | unsafe { Box::from_raw(raw as *mut $t) }
> | ^^^^^^^^^^^^^^
> ...
> 145 | impl_clone!(dyn CloneAny + Send + Sync);
> | --------------------------------------- in this macro invocation
> |
> = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
> = note: for more information, see issue #127323 <https://github.com/rust-lang/rust/issues/127323>
> = note: this warning originates in the macro `impl_clone` (in Nightly builds, run with -Z macro-backtrace for more info)
Could not compile anymap
.
--- stderr
src/lib.rs:56:41: 56:44 error: explicit lifetime bound required
src/lib.rs:56 impl<'a> UncheckedAnyRefExt<'a> for &'a Any {
^~~
src/lib.rs:74:48: 74:51 error: explicit lifetime bound required
src/lib.rs:74 impl<'a> UncheckedAnyMutRefExt<'a> for &'a mut Any {
^~~
src/lib.rs:111:31: 111:34 error: explicit lifetime bound required
src/lib.rs:111 data: HashMap<TypeId, Box, TypeIdHasher>,
^~~
error: aborting due to 3 previous errors
[root@localhost nickel]# rustc -v
rustc 0.12.0-pre-nightly (5419b2ca2 2014-08-29 22:16:20 +0000)
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.