Reply to post: Re: Are you sure?

In Rust we trust: Shoring up Apache, ISRG ditches C, turns to wunderkind lang for new TLS crypto module

ssokolow

Re: Are you sure?

It doesn't strictly have anything to do with reference counted pointers, just that there is a reason that types other than atomics require those sorts of explicit conversions. There exist valid use cases for non-atomics where truncation of such integers might induce memory unsafety and Rust is designed around giving you as much ability as is reasonably possible to rule out abuse of your APIs at compile time.

As for typeclasses, my advice is to only turn to Haskell material as a last resort. It tends to be like consulting Wikipedia about mathematics.

I mentioned the name in case you were already familiar with them, but, if not, "a cross between interfaces and abstract classes" is a good enough conceptual starting point as long as you understand that structs in Rust are always plain old data.

Monomorphic/static dispatch is done by having methods be syntactic sugar for functions which take the struct as the first argument and dynamic dispatch is done by storing the vtable separately and using a fat pointer instead.

The Box<dyn Trait> and &dyn Trait parts of this slide explain that visually:

https://docs.google.com/presentation/d/1q-c7UAyrUlM-eZyTo1pd8SZ0qwA_wYxmPZVOQkoDmH4

As for your example code, I'll give three implementations, because that kind of parallelism is typically handled using one of two libraries in real-world code, but you probably want to see it done using the primitives.

First, Rayon, which uses Rust's support for implementing new traits on other people's types to add parallel iterators:

https://play.rust-lang.org/?version=stable&mode=release&edition=2018&gist=b933ba75d1db68e5f69009aa3e41501a

Second, Crossbeam, which provides an abstraction over the `unsafe` code necessary to assure the compiler that your threads won't outlive the backing store so you can hand out immutable slices without needing to maintain a reference count.

https://play.rust-lang.org/?version=stable&mode=release&edition=2018&gist=3cacfac45bf302019efd19df481a7778

Third, using the Rust equivalent of std::shared_ptr and a less functional style closer to your code:

https://play.rust-lang.org/?version=stable&mode=release&edition=2018&gist=9352a28cb8c111ee644ad1a8f6ce46b3

Apologies if any of these are sub-optimal. It's late and I spend most of my time writing I/O-bound code, so I haven't yet built up the strongest intuition for where LLVM's optimizers might fall short and I'm starting to get drowsy, so I didn't throw them into godbolt to check.

(Because I'm getting drowsy, I'll have to reply to the rest of what you said tomorrow.)

...but thanks for the video. I'll see if I can find time for it within the next little while. (Lately, most of my video watching time has been for stuff where it's OK to listen with half an ear while I do something else.)

POST COMMENT House rules

Not a member of The Register? Create a new account here.

  • Enter your comment

  • Add an icon

Anonymous cowards cannot choose their icon