Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

no_std support for the url crate #831

Open
wants to merge 30 commits into
base: main
Choose a base branch
from
Open

Conversation

domenukk
Copy link
Contributor

@domenukk domenukk commented Apr 8, 2023

This extends/rebases/fixes #717.
All checks seem to pass.
For no_std support, however, as mentioned in #717, this still needs a bunch of fixes. Specifically, It looks like we'll have to use the ip_in_core nightly feature, or use this crate: https://docs.rs/no-std-net. I'd personally opt for nightly, as it looks like it will be merged eventually, but I could do both.

@domenukk
Copy link
Contributor Author

domenukk commented Apr 8, 2023

Went for ip_in_core now, so it needs nightly, but seems to work.

@domenukk domenukk marked this pull request as ready for review April 8, 2023 16:09
///
/// # fn run() -> Result<(), Box<dyn Error>> {
/// # use url::ParseError;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not super pretty. If URL had a no_std error type that also supports strings, it could be nicer. However, it's not visible in docs so should be fine

@domenukk
Copy link
Contributor Author

I spent some time trying to get serde no_std support working, but it doesn't play nicely with the ip_in_core nightly feature - it throws errors like

the trait `Serialize` is not implemented for `Ipv6Addr`

@valenting
Copy link
Collaborator

Went for ip_in_core now, so it needs nightly, but seems to work.

If it breaks stable, that's obviously a no-go.
Also, our msrv is 1.56.0, so preferably the no_std changes would also work on that version.

@codecov
Copy link

codecov bot commented Apr 10, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (92f356e) 81.70% compared to head (316c868) 81.70%.

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #831   +/-   ##
=======================================
  Coverage   81.70%   81.70%           
=======================================
  Files          20       20           
  Lines        3547     3547           
=======================================
  Hits         2898     2898           
  Misses        649      649           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@domenukk
Copy link
Contributor Author

It should not break stable, it should only "break" no_std (which never worked in the first place).
Switching to no-std-net also doesn't fix the problem with serde - the project seems stale/would need this to be merged:
dunmatt/no-std-net#16

@domenukk
Copy link
Contributor Author

Moving to no-std-net anyway, since it seems to fit your msrv better

@domenukk
Copy link
Contributor Author

Since I do need the nightly version, but I can see why some people wouldn't, I added two features:

  • no_std_net uses the no_std_net crate, no nightly needed (but the extra dependency)
  • unstable enabled unstable net_in_core feature, and also the error_in_core feature. Due to its opt-in nature, I would assume this is an okay solution (?)

Else we can wait until the features get stabilised.
Feedback welcome.

@@ -453,7 +453,7 @@ impl Idna {
return Errors::default();
}
let mut errors = processing(domain, self.config, &mut self.normalized, out);
self.output = std::mem::replace(out, String::with_capacity(out.len()));
self.output = core::mem::replace(out, String::with_capacity(out.len()));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI, this fixes a bug in the current idna no_std support. Why is it not caught in CI, and should I open an extra PR for it?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recently dabbled with no_std stuff as well and I think the sure-fire way to test no_std compatibility is to:

  • target a platform that does not have std (certain ARM platforms) OR
  • have a really simple no_std binary crate which uses the libraries

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I'm trying it with cargo +nightly build -Zbuild-std=core,alloc --target aarch64-unknown-none -v --release --no-default-features

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you split this off into a separate PR?

@domenukk
Copy link
Contributor Author

Any news on this? :)

@@ -453,7 +453,7 @@ impl Idna {
return Errors::default();
}
let mut errors = processing(domain, self.config, &mut self.normalized, out);
self.output = std::mem::replace(out, String::with_capacity(out.len()));
self.output = core::mem::replace(out, String::with_capacity(out.len()));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you split this off into a separate PR?

url/Cargo.toml Show resolved Hide resolved
@domenukk
Copy link
Contributor Author

domenukk commented Jun 14, 2023

Opened #843 to fix the idna no_std bug.

@lucacasonato lucacasonato mentioned this pull request Jul 11, 2023
4 tasks
@bors-servo
Copy link
Contributor

☔ The latest upstream changes (presumably #853) made this pull request unmergeable. Please resolve the merge conflicts.

@bors-servo
Copy link
Contributor

☔ The latest upstream changes (presumably beb2cde) made this pull request unmergeable. Please resolve the merge conflicts.

if: |
matrix.os == 'ubuntu-latest' &&
matrix.rust == 'nightly'
run: rustup target add aarch64-unknown-none && rustup component add rust-src --toolchain nightly-x86_64-unknown-linux-gnu && cargo +nightly build -Zbuild-std=core,alloc --target aarch64-unknown-none -v --release --no-default-features --features=alloc,unstable
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lucacasonato you could take over this check to the idna create

@domenukk
Copy link
Contributor Author

Apparently building --no-default-features workspace-wide didn't work, moved that check to the url create only -> CI might turn green

@domenukk
Copy link
Contributor Author

It's green 🎉

@domenukk
Copy link
Contributor Author

Any news on this? @valenting

@domenukk
Copy link
Contributor Author

Any hint on what to do with codecov CI?

Copy link
Contributor

@madsmtm madsmtm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the guiding principle for this feature should be: "Don't inconvenience people who don't care about no_std", which is... All of url's current users ;)

So, I really don't think we should bump MSRV for this feature - but a way to solve that would be to use rustc_version in a build script to detect when the feature is available. (This does come with a slight compile-time cost, but hopefully that's acceptable, and it will go away once the functionality is in url's MSRV).


[features]
default = []
default = ["std"]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fear this might be a fairly big breaking change?

When I added no_std support for the other libraries in this crate, it was more trivial, since there was only three crates that disabled default features - but that is not the case for url; there are many crates specifying default-features = false, so even though there was no default features previously, this change will affect all of those crates.

Python script for finding crates using `default-features = false`.
import json
import requests
import sys

API_URL = "https://crates.io/api/v1/crates"

crate_name = sys.argv[1]

page = 1
while True:
    # print(f"page {page}")
    data = json.loads(requests.get(f"{API_URL}/{crate_name}/reverse_dependencies?per_page=100&page={page}").content)
    page += 1

    if len(data['versions']) == 0:
        break

    versions = {x['id']: x for x in data['versions']}

    for dep in data['dependencies']:
        if not dep['default_features']:
            crate = versions[dep['version_id']]
            print(f"{crate['crate']} had `default-features = false`")

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see any other way?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well... I see two:

  • Bundling this in with other breaking changes, and releasing url v0.3
  • Calling the feature unstable-no-std instead, to signify that it may break other crates in the dependency chain, and that it is a bad way of doing it which will be rectified once v0.3 is released.

no_std_net = ["no-std-net"]
# UNSTABLE FEATURES (requires Rust nightly)
# For no_std: Use errors_in_core and net_in_core
unstable = ["idna/unstable"]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rust-lang/rust#108443 is FCP-ing now, so it'll probably land in a few months - perhaps we should wait with this feature until then?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right now this PR also uses error_in_core (rust-lang/rust#103765) but that part can be removed if we want to go for stable instead

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The value in that feature is that this could become a non-breaking change (other than bumping MSRV, which is probably acceptable).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update: core::net has landed in Rust 1.77.0

url/Cargo.toml Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants