11 ms·
Gnirehtet rewritten in Rust
- steveklabnik 9y agoApparently the name is "tethering" backwards.
- saghm 9y agoEmbarrassingly, it took me until halfway thorough the article to notice this
- matt_wulfeck 9y ago> At Genymobile, we wanted Gnirehtet not to require the Java Runtime Environment, so the main requirement was to compile the application to a native executable binary. While I think that rust is a great choice, if multi platform static executables is your goal then I think you missed a language that does such a thing extrmely well. I’m surprised the author didn’t even consider Go for this purpose.
- stouset 9y agoYou may have missed these three points the author listed, favoring Rust: * memory safety without garbage collection, * concurrency without data races, * abstraction without overhead Go has garbage collection, doesn’t statically prevent data races, and somewhat infamously allows for only minimal abstraction.
- lossolo 9y agoYou should reread article again: > But at that time (early May), I was interested in learning Rust, after vaguely hearing what it provided, namely: And then he listed what you listed in your comment. As he wrote the main requirement was not to include JRE. This could also be achieved by using Go.
- dimfeld 9y agoI suspect that "But at that time (early May), I was interested in learning Rust" was a big consideration there. But I can also vouch for Go's excellent cross-compilation support.
- steveklabnik 9y agoRust is also pretty good at this, not quite as good as Go, but still generally pretty solid. Main related points: Rust code is statically linked by default. C code you link to may not be. Many -sys packages (that wrap C libraries) give you an option to link statically or dynamically. libc isn't statically linked by default, so that's usually the only thing not-statically linked in a pure-Rust program. You can use musl instead if you'd like.
- Ruud-v-A 9y agoHaskell is another language that compiles to native executables with minimal dependencies (binaries do link against libc, libz, and libgmp by default) which I think deserves more attention. It has great build tooling too (Stack). I have been responsible for building and deploying Python, JVM, and Haskell projects to production servers. For Haskell projects it was pretty much painless, Python and JVM were "challenging". The main advantage comes from having native binaries and a good build system that does the right thing, which Rust is also good at.
- masklinn 9y ago> Haskell is another language that compiles to native executables with minimal dependencies (binaries do link against libc, libz, and libgmp by default) which I think deserves more attention. OCaml hits most of the boxes and would be more convenient/familiar to most users since it's strict and impure.
- solidsnack9000 9y agoThe discussion of how borrowing impacts encapsulation, abstraction with private methods, and the observer pattern was informative and interesting.
- w4tson 9y agoI found it useful. I think there’s a missing guide out there for people like myself who have an OO background, are looking to write some rust but can’t reconcile the borrow-checker with how they normally design software
- juanpotato 9y agohttps://doc.rust-lang.org/nightly/book/second-edition/ch04-01-what-is-ownership.html https://doc.rust-lang.org/nightly/book/second-edition/ch04-0... try this
- deleted 9y ago[deleted]
- moomin 9y agoIt's interesting, but overstated. It's more a description of how some Java patterns don't translate. For instance, the first example "encapsulation" demonstrates exactly no encapsulation. In general terms although sometimes you need to do things like this, shared mutable state is something you should be avoiding in any language and you should thank Rust for making it harder on you.
- solidsnack9000 9y ago> ...the first example "encapsulation" demonstrates exactly no encapsulation. It demonstrates a kind of getter, but a weird one since it returns a mutable field. These are not the most encapsulated thing.
- oconnor663 9y agoI think the part where they're trying to abstract out a `content` method could be made to work by adding braces, so that the `&self` borrow goes away earlier than the end of the function. I think "nonlexical lifetimes" are also going to make more cases like that work automatically without braces soon.
- akavel 9y agoIs there some valuable disscussion of the article on rust subreddit? If yes, could somebody please link it here?
- txru 9y agohttps://www.reddit.com/r/rust/comments/71ks57/gnirehtet_a_reverse_tethering_tool_for_android/ https://www.reddit.com/r/rust/comments/71ks57/gnirehtet_a_re...
- deleted 9y ago[deleted]
- viraptor 9y agoI think in the encapsulation / content example they missed the possibility of creating an explicit block on your own. I believe something like this would solve it: let new_sum = { calculate it from self.content() } self.sum = new_sum; Since the sum value itself doesn't borrow anymore.
- rom1v 9y agoYes, you're right, this particular (artificial) example could be rewritten easily so that it compiles. But the point was to show that a compiling program may not compile anymore after factoring some code into a private method.
- mathw 9y agoIt's a useful lesson in that Rust's correctness relies on certain notions of borrowing dependencies which don't necessarily match what you assume. Especially that the lifecycle dependency across a function call is essentially opaque to the compiler. Which is how it has to be, or you could never state anything sane about the lifecycle when you return a reference.
- fnord123 9y agoIs the purpose of encapsulation to wrap all data fields in functions offering mutable references to data? I thought it was the exact opposite.
- rom1v 9y agoThe purpose was to show that something lifetime-bound to a part of a struct becomes lifetime-bound to the whole struct when returned from a function. Using mutable references is the minimal way to get this.
- im_down_w_otp 9y agoWhen I write Rust I tend to do so using functional/procedural patterns instead of object-oriented ones, and it doesn't seem like I run into issues like the ones described here very often. Much of this write-up seems to cover things that were/are complicated because of OOP's tight coupling of state and actions on that state & the intersection of that with the additional constraints and controls Rust places on state management. I'm not entirely sure which style/patterns are more idiomatic. I just know which ones keep me out of trouble.
- bluejekyll 9y agoI don’t know about this. I think the trait system is so powerful of a concept for genetics and such that I find that I practice OO pretty regularly. I avoid dynamic polymorphism, but most OO practices translate really well into Rust.
- sixbrx 9y agoSounds closer to type classes usage a la Haskel than what I'd call OO (late binding)?
- bluejekyll 9y agoI try to capture data in a enum or struct, then define the operations available to that type. A trait allows me to capture common functionality across different types which implement that trait. With generics it allows me to be polymorphic. If that’s not OO, I don’t know what is :)
- Tarean 9y agoWell, technically traits dispatch based on type and not on value. As long as you aren't wrapping everything into trait objects that is much closer to functional programming. Seems like a mostly semantical difference, though. I guess there is a philosophical difference in whether you pass a large struct into a function or select the needed data at the call side and only pass that, though. Former might be easier to extend, latter is probably easier to test?
- pjmlp 9y ago> ... I introduced Gnirehtet, a reverse tethering tool for Android I wrote in Java. > At Genymobile, we wanted Gnirehtet not to require the Java Runtime Environment, so the main requirement was to compile the application to a native executable binary. While it is nice to see new projects adopting Rust, I really don't get the point of having a tool targeted to Android developers not depending on Java.
- harrisi 9y agoAs far as I can tell it's not necessarily targeted at Android developers. It seems like the primary use case is if you have some device with cell service and you're out of WiFi range but want to use your Android device with an internet connection. They must have customers that are in that situation somewhat regularly (traveling business people?) and keeping the overhead low is desired.
- mathw 9y agoThe "encapsulation" example is an interesting one, because it sits right in the fundamental difference between a Rust reference and a Java one, and the lifecycle considerations of a garbage-collected language vs one where lifecycles are statically determined by the compiler. I guess one way to explain it, if one needed to, is to say that the Java object is effectively a collection of Rc<RefCell<T>>, and getters return copies of those Rc<>s. (Okay I know, garbage collection is not the same as refcounting, but in this kind of analysis it's effectively the same). Makes me sit back and think how much work Java the language is doing for you here.
- tango25 9y agoHello Gnirehtet is really a great tool. Is there a possibility to have the RUST Version compiled for an ARM architecture? I'm running it on a Raspi3 as a tether box. Obviously less CPU consumption would be an enormous advantage. , since with the java version transmit speed only reaches arround 50 kbit/s. Thank you very much for your great work!
- steveklabnik 9y agoIt should work, yeah.
- tango25 9y agoAwesome! will you put a executable at GitHub like the others? Thank you very much!
- steveklabnik 9y agoI'm not the maintainer of the project, so I can't do that. Rust can cross-compile to that architecture though, so it should be possible, hence my reply. You probably should file an issue so the maintainer actually sees it.
- tango25 9y agoHere it is! Awesome! https://github.com/Genymobile/gnirehtet/issues/46 https://github.com/Genymobile/gnirehtet/issues/46