GithubHelp home page GithubHelp logo

Comments (10)

briansmith avatar briansmith commented on May 19, 2024 1

Now in ring::signature we are using traits. And, of course, it creates a problem: We cannot always say &'static signature::Algorithm but instead have to say &'static (signature::Algorithm + Sync) when referencing algorithms from other statics, because of Rust type system weirdness.

And, it was so awkward I ended up implementing wrapper types to hide the traits.

Separately, I already had to explain this a bit on IRC:

briansmith> right now, you can use ring with "use ring;" or "use ring::{submodule, submodule, submodule};" and everything works.
20:30 But, as soon as you use traits, that stops working, because you need to import the traits too.
20:31 And also, in ring we usually use polymorphism to select between algorithms.
20:31 And so, usually, every implementation of each trait would only have one implementation, each.
20:32 So, instead, in ring I basically emulate per-object (vs per-trait) polymorphism.
20:34 IMO, it is something that should be fixed in the language: when a type implements a trait, it should be able to say "when I am in scope, the trait should also be in scope."
20:34 but, that's not the case yet.
briansmith

from ring.

briansmith avatar briansmith commented on May 19, 2024

Reconsider this after the placement box stuff is stable: rust-lang/rfcs#1228

from ring.

briansmith avatar briansmith commented on May 19, 2024

It looks like the whole placement new stuff is being tracked in rust-lang/rust#27779. I asked about how to resolve the issues that ring has run into with Traits in rust-lang/rust#27779 (comment).

from ring.

briansmith avatar briansmith commented on May 19, 2024

See also https://www.ralfj.de/blog/2015/10/12/formalizing-rust.html: Traits are not in the subset of Rust that they are hoping to define semantics for and/or prove correct.

from ring.

briansmith avatar briansmith commented on May 19, 2024

See https://internals.rust-lang.org/t/pre-rfc-thinness-as-a-property-of-the-data-rather-than-the-trait/2794, https://internals.rust-lang.org/t/blog-post-extended-enums-and-thin-traits/2755, https://internals.rust-lang.org/t/blog-post-extended-enums-and-thin-traits/2755, http://smallcultfollowing.com/babysteps/blog/2015/10/08/virtual-structs-part-4-extended-enums-and-thin-traits/.

from ring.

briansmith avatar briansmith commented on May 19, 2024

Now in ring::signature we are using traits. And, of course, it creates a problem: We cannot always say &'static signature::Algorithm but instead have to say &'static (signature::Algorithm + Sync) when referencing algorithms from other statics, because of Rust type system weirdness.

from ring.

Diggsey avatar Diggsey commented on May 19, 2024

It looks like VerificationAlgorithm extends Sync now - doesn't that solve the problem with verbosity? And is the requirement to import traits separately really a show-stopper?

With function pointers, the algorithm structs become much larger than the double-pointer sized &'static Algorithm, and it becomes difficult to add more trait implementations like Debug in future, without introducing even more inefficiencies. It also prevents downstream crates from extending the set of possible algorithms.

From a security perspective, storing multiple function pointers in writable locations on the stack also seems much more dangerous than a reference to a vtable in read-only memory.

from ring.

briansmith avatar briansmith commented on May 19, 2024

It looks like VerificationAlgorithm extends Sync now - doesn't that solve the problem with verbosity?

Yes.

And is the requirement to import traits separately really a show-stopper?

It's a real usability problem. But, it's not worse in ring than it is in other APIs.

With function pointers, the algorithm structs become much larger

It doesn't matter, because there is only one copy of every Algorithm, so I don't think it creates any bloat.

it becomes difficult to add more trait implementations like Debug in future

Not really, because the best implementation of Debug for Algorithm types is just to output the name of the algorithm and omit the details anyway.

It also prevents downstream crates from extending the set of possible algorithms.

True. OTOH, we don't have to worry about external crates' implementations when we refactor the code, because of this. People can contribute implementations of new algorithms to ring if they want more algorithms supported.

From a security perspective, storing multiple function pointers in writable locations on the stack also seems much more dangerous than a reference to a vtable in read-only memory.

Very true, but the compiler should be able to put them in read-only memory since they are all static and not static mut, just like trait vtables. I'm not sure if the compiler actually does this, though.

from ring.

burdges avatar burdges commented on May 19, 2024

There is an RFC somewhere to allow types to bring trait methods into scope. I forget where or what the syntax is, but maybe something like struct MyStruct: MyTrait+Sync where ... And no movement recently.

from ring.

Diggsey avatar Diggsey commented on May 19, 2024

It doesn't matter, because there is only one copy of every Algorithm, so I don't think it creates any bloat.

Ah, some of my arguments were based on the mistaken assumption that Algorithms were passed around by value.

from ring.

Related Issues (20)

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.