-
-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Mark Id
as thread-safe
#2
Comments
Thanks a lot for the thorough post! I think this makes sense; the only issue is that I do not want to introduce any We could use |
Thanks for the swift reply! I think I will test this approach in my code (which requires |
Another variant, I think, could be
|
I don't think You could argue |
(sorry for the repost)
Suggestion:
Change the implementation of
Id<T>
to hold a marker of typePhantomData<*const T>
instead ofPhantomData<T>
, and to manually implementSend + Sync
forId<T>
.Rationale:
Ultimately,
Id<T>
semantically behaves just like an index into some storage to retrieve an object of typeT
. In this regard, passing around anId<T>
should be no different than passing around ausize
.However, because
Id<T>
usesPhantomData<T>
, the Rust compiler thinks thatId<T>
actually owns an object of typeT
, thus imposing the same thread-safety restrictions onId<T>
, as if it actually heldT
.I believe, this is semantically not correct, and in this case,
Id
could be marked asSend + Sync
regardless of the type of data it references, in addition to changing the definition insideId<T>
fromPhantomData<T>
toPhantomData<*const T>
to relieveId<T>
from having to obey by the lifetime restrictions ofT
.Here is the relevant excerpt from the Rust docs on this practice:
Use cases:
This is especially useful if you want an ID to data that is stored in some system somewhere that isn't
Send
orSync
, or has certain lifetime restrictions considerably complicating the API.I believe it is appropriate to do so, because you cannot acquire an object of type
T
using just theId<T>
, you also need the relevantIdMap
orIdSlab
, which would in turn impose the correct lifetime and thread-safety restrictions upon trying to retrieve the actual object.Ultimately, this would allow other systems (and threads!) to hold IDs to data of the relevant system, without being burdened by the lifetimes and thread-safety restrictions of the original type.
Please, correct me if my reasoning is wrong and if it could potentially introduce UB into the code. Otherwise, this should be a very simple change and should not break any backwards compatibility (I have used
rust-doom
as an example to verify that).I can open a PR with the change if you approve of the concept.
The text was updated successfully, but these errors were encountered: