Coder Social home page Coder Social logo

Comments (4)

cristicbz avatar cristicbz commented on July 23, 2024

Thanks a lot for the thorough post! I think this makes sense; the only issue is that I do not want to introduce any unsafe (which this would do for the manual impls for Send and Sync).

We could use &'static T, but that would require the T: 'static bound, which isn't great. What about doing PhantomData<fn(T) -> T>: this makes Id<T>: Send + Sync and invariant in T which I think is a good idea.

from idcontain.rs.

ElusiveMori avatar ElusiveMori commented on July 23, 2024

Thanks for the swift reply!

I think PhantomData<fn(T) -> T> may look like a bit of a hack, but I definitely understand trying to avoid unsafe, and I personally can't think of any more alternatives. It would probably be worth leaving a comment about this in the code, too.

I will test this approach in my code (which requires Id<T> to be thread safe and without lifetime bounds), verify that it compiles, and report back.

from idcontain.rs.

ElusiveMori avatar ElusiveMori commented on July 23, 2024

Another variant, I think, could be PhantomData<Arc<Mutex<*const T>>>, but it is hardly less hacky than just PhantomData<fn(T) -> T>, although it could be argued that it conveys the intent better:

  • Arc + Mutex provide Send + Sync traits
  • *const T ignores lifetimes.

from idcontain.rs.

cristicbz avatar cristicbz commented on July 23, 2024

I don't think fn(T) -> T is that hacky; it's what https://doc.rust-lang.org/nomicon/phantom-data.html recommends for non-lifetime bound invariance.

You could argue fn() -> T is actually how an Id behaves: it's a function that given an IdSlab it returns a T.

from idcontain.rs.

Related Issues (1)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.