7

How to Overriding (Patch) Cargo Dependencies

 2 years ago
source link: https://edger.substack.com/p/how-to-overriding-patch-cargo-dependencies
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

What’s the purpose?

I’ve been working on a Rust Bevy project for a couple of monthes, the WASM target runs quite smooth in desktop browsers, though on mobile, it’s not working, the rendering seems fine, the biggest issue is input for now, the touch screen just not working at all.

Did spend some time doing research, there are logic in Bevy for touch input, so I updated my application to use them, though still not working.

A few more searches leads me to the winit repo, which is the underlining library to support window managing, input handling... the touch input on WASM is not implemented yet.

Implement touch handling on web target(s) · Issue #1673 · rust-windowing/winit (github.com)

Good news is someone actually created a pull request for this.

Add support for `event::Touch` on web targets by dsluijk · Pull Request #1945 · rust-windowing/winit (github.com)

So I need some way to use the pull request in my project, it’s not as easy as I thought, so end up with this post.

(Won’t discuss about the actual work on the touch support, you can check this branch for details: feature-touch_wasm_mobile)

Specifying git dependency

So I did some research on Cargo manual. seems I just need to specify a git dependency, here is the manual.

Specifying Dependencies - The Cargo Book (rust-lang.org)

So I added this to Cargo.toml

[dependencies]
winit = { git = "https://github.com/yjpark/winit", branch = "touch-input_0.24.0" }

Compiled fine, created a new WASM build, testing on iPad, nothing changed.

Specified the wrong dependency

Well, after some thinking, realise that I just did specified the wrong version, Since it’s Bevy’s codes are actually using wint functionality, I MUST let bevy to use the right version in it’s dependencies.

So I forked bevy, change it’s winit dependency to use the above branch, and change my application to something like this:

[dependencies]
bevy = { git = "https://github.com/yjpark/bevy", branch = "touch-input_0.5.0" }

Well, this time, it won’t compile, got a lot of errors.

Why it’s not working?

Of course it’s not working, I’m using multiple library on top of Bevy, e.g.

Nilirad/bevy_prototype_lyon: Draw 2D shapes in Bevy (github.com)

mvlabat/bevy_egui: A plugin for Egui integration into Bevy (github.com)

Since they are depending on the official version of Bevy v0.5.0, the winit codes in there are based on the original one.

At this time, I started to get frustrated, it felt like a dependency hell, even with all the source codes there, I just can’t get it compiled, is it a bad design in Cargo? or is it my problem not reading manual?

It’s indeed in the manual

So I did some more readings, luckily just under the last page in the Cargo Book, this page saved my day.

Overriding Dependencies - The Cargo Book (rust-lang.org)

Turns out what I need was NOT Specifying Dependencies, instead I need to Overriding Dependencies.

[patch.crates-io]
winit = { git = "https://github.com/yjpark/winit/", branch = "touch-input_0.24.0" }

In this way, all the libraries won’t need to be forked, but when Rust compiling the code, will just use this version for all the places when winit is needed, brilliant!

Retrospect

This is a good example for why software development is hard, just too much information, too many manuals to read, and the subtle diffrences in concepts are critical for problem solving.

On the other hand, the Carge Book is very well written, all the libraries are open sourced, so we can get problem solved without too much difficulties, so I’m glad and grateful.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK