3 ms·
> Don't you still have Iterator invalidation to keep track of and I thought I saw posts regretting using `std::string_view` because it is easy to reference dele
by jonstewart 4y ago
> Don't you still have Iterator invalidation to keep track of and I thought I saw posts regretting using `std::string_view` because it is easy to reference deleted memory.
I don't run into iterator invalidation that much in practice. I'm not often erasing elements from collections. There are all sorts of things like std::string_view where you could reference bad memory if you don't manage lifetimes, but I... just... come up with designs where lifetimes are managed properly? It's probably habit and very intuitive for me, hardly conscious of it.
Also, this is where the static analyzer is fantastic. If you have good unit test coverage, it will let you know you've screwed up and pinpoint where.
I don't mind the borrow checker as a default, but it would be nice for an escape hatch that wasn't an unsafe{} block. Perhaps one with runtime checks? Predictable conditions are virtually free with branch prediction.
> Recently, I've seen an inverted pattern for this, define a trait and `impl MyTrait for Fn`. I've found I much prefer this pattern over the cases where I would have used a functor. Granted, there are more ad-hoc cases where functors would be better.
Can you give an example or provide a citation to this? I'd love to read more.
- Rusky 4y ago> Can you give an example or provide a citation to this? It would look something like this: trait MyOperation { fn run(&mut self, a: T, b: U, c: V) -> R; } impl<F> MyOperation for F where F: FnMut(T, U, V) -> R { fn run(&mut self, a: T, b: U, c: V) -> R { self(a, b, c) } } fn my_api<F: MyOperation>(f: F) { ... f.run(a, b, c) ... } struct MyFunctor { ... } impl MyOperation for MyFunctor { ... } my_api(MyFunctor { ... }); That is, when defining `my_api` and you would like to accept both lambdas and functors, instead of using a `Fn` trait bound directly, use a more specific trait which has a blanket impl for lambdas.
- epage 4y ago> Can you give an example or provide a citation to this? I'd love to read more. `nom`'s `Parser` trait is implemented for functions. You can mix and match parser functions and parser types, including the built-in types that are returned when you call the post-fix combinators on the `Parser` trait. https://docs.rs/nom/latest/nom/ https://docs.rs/nom/latest/nom/