-
Notifications
You must be signed in to change notification settings - Fork 308
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
base: main
Are you sure you want to change the base?
Changes from 9 commits
35653b0
194122c
422a260
81ee074
67d2a4c
db92dd2
bb7955a
a98aef8
0d14d5a
315eb43
6d622ab
3efe254
36bc5b3
29bfcf7
e16eb2b
3dd87d8
af1d5d5
b908f7f
fd98554
b0bb942
047d334
028424a
7073957
be123af
c145584
be73851
96e2a04
268c17e
0c02cd9
316c868
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,13 +28,15 @@ debugger_test = "0.1" | |
debugger_test_parser = "0.1" | ||
|
||
[dependencies] | ||
form_urlencoded = { version = "1.1.0", path = "../form_urlencoded" } | ||
idna = { version = "0.3.0", path = "../idna" } | ||
percent-encoding = { version = "2.2.0", path = "../percent_encoding" } | ||
form_urlencoded = { version = "1.1.0", path = "../form_urlencoded", default-features = false } | ||
idna = { version = "0.3.0", path = "../idna", default-features = false } | ||
percent-encoding = { version = "2.2.0", path = "../percent_encoding", default-features = false } | ||
serde = {version = "1.0", optional = true, features = ["derive"]} | ||
|
||
[features] | ||
default = [] | ||
default = ["std"] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 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`") There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't see any other way? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well... I see two:
|
||
std = ["idna/std", "percent-encoding/std", "form_urlencoded/std", "alloc"] | ||
alloc = [] | ||
# UNSTABLE FEATURES (requires Rust nightly) | ||
# Enable to use the #[debugger_visualizer] attribute. | ||
debugger_visualizer = [] | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -127,27 +127,47 @@ url = { version = "2", features = ["serde"] } | |
feature(debugger_visualizer), | ||
debugger_visualizer(natvis_file = "../../debug_metadata/url.natvis") | ||
)] | ||
#![no_std] | ||
#![cfg_attr(not(feature = "std"), feature(ip_in_core))] | ||
|
||
pub use form_urlencoded; | ||
|
||
// For forwards compatibility | ||
#[cfg(feature = "std")] | ||
extern crate std; | ||
|
||
#[macro_use] | ||
extern crate alloc; | ||
|
||
#[cfg(not(feature = "alloc"))] | ||
compile_error!("the `alloc` feature must be enabled"); | ||
|
||
#[cfg(feature = "serde")] | ||
extern crate serde; | ||
|
||
use crate::host::HostInternal; | ||
use crate::parser::{to_u32, Context, Parser, SchemeType, PATH_SEGMENT, USERINFO}; | ||
use percent_encoding::{percent_decode, percent_encode, utf8_percent_encode}; | ||
use std::borrow::Borrow; | ||
use std::cmp; | ||
use std::fmt::{self, Write}; | ||
use std::hash; | ||
use std::io; | ||
use std::mem; | ||
use std::net::{IpAddr, SocketAddr, ToSocketAddrs}; | ||
use std::ops::{Range, RangeFrom, RangeTo}; | ||
use std::path::{Path, PathBuf}; | ||
use std::str; | ||
|
||
use std::convert::TryFrom; | ||
use crate::parser::{to_u32, Context, Parser, SchemeType, USERINFO}; | ||
use alloc::borrow::ToOwned; | ||
use alloc::string::{String, ToString}; | ||
use core::borrow::Borrow; | ||
use core::cmp; | ||
use core::convert::TryFrom; | ||
use core::fmt::{self, Write}; | ||
use core::hash; | ||
use core::mem; | ||
#[cfg(not(feature = "std"))] | ||
use core::net::IpAddr; | ||
use core::ops::{Range, RangeFrom, RangeTo}; | ||
use core::str; | ||
use percent_encoding::utf8_percent_encode; | ||
#[cfg(feature = "std")] | ||
use std::net::IpAddr; | ||
#[cfg(feature = "std")] | ||
use std::{ | ||
io, | ||
net::{SocketAddr, ToSocketAddrs}, | ||
path::{Path, PathBuf}, | ||
}; | ||
|
||
pub use crate::host::Host; | ||
pub use crate::origin::{OpaqueOrigin, Origin}; | ||
|
@@ -1237,10 +1257,11 @@ impl Url { | |
/// }) | ||
/// } | ||
/// ``` | ||
#[cfg(feature = "std")] | ||
pub fn socket_addrs( | ||
&self, | ||
default_port_number: impl Fn() -> Option<u16>, | ||
) -> io::Result<Vec<SocketAddr>> { | ||
) -> io::Result<alloc::vec::Vec<SocketAddr>> { | ||
// Note: trying to avoid the Vec allocation by returning `impl AsRef<[SocketAddr]>` | ||
// causes borrowck issues because the return value borrows `default_port_number`: | ||
// | ||
|
@@ -1249,6 +1270,7 @@ impl Url { | |
// > This RFC proposes that *all* type parameters are considered in scope | ||
// > for `impl Trait` in return position | ||
|
||
// TODO: Return custom error type to support no_std | ||
fn io_result<T>(opt: Option<T>, message: &str) -> io::Result<T> { | ||
opt.ok_or_else(|| io::Error::new(io::ErrorKind::InvalidData, message)) | ||
} | ||
|
@@ -1310,9 +1332,23 @@ impl Url { | |
/// | ||
/// ``` | ||
/// use url::Url; | ||
/// # use std::error::Error; | ||
/// | ||
/// # fn run() -> Result<(), Box<dyn Error>> { | ||
/// # use url::ParseError; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not super pretty. If URL had a |
||
/// # #[derive(Debug)] | ||
/// # /// A simple wrapper error struct for `no_std` support | ||
/// # struct TestError; | ||
/// # impl From<ParseError> for TestError { | ||
/// # fn from(value: ParseError) -> Self { | ||
/// # TestError {} | ||
/// # } | ||
/// # } | ||
/// # impl From<&str> for TestError { | ||
/// # fn from(value: &str) -> Self { | ||
/// # TestError {} | ||
/// # } | ||
/// # } | ||
/// | ||
/// # fn run() -> Result<(), TestError> { | ||
/// let url = Url::parse("https://example.com/foo/bar")?; | ||
/// let mut path_segments = url.path_segments().ok_or_else(|| "cannot be base")?; | ||
/// assert_eq!(path_segments.next(), Some("foo")); | ||
|
@@ -1717,9 +1753,22 @@ impl Url { | |
/// | ||
/// ``` | ||
/// use url::Url; | ||
/// # use std::error::Error; | ||
/// # use url::ParseError; | ||
/// # #[derive(Debug)] | ||
/// # /// A simple wrapper error struct for `no_std` support | ||
/// # struct TestError; | ||
/// # impl From<ParseError> for TestError { | ||
/// # fn from(value: ParseError) -> Self { | ||
/// # TestError {} | ||
/// # } | ||
/// # } | ||
/// # impl From<&str> for TestError { | ||
/// # fn from(value: &str) -> Self { | ||
/// # TestError {} | ||
/// # } | ||
/// # } | ||
/// | ||
/// # fn run() -> Result<(), Box<dyn Error>> { | ||
/// # fn run() -> Result<(), TestError> { | ||
/// let mut url = Url::parse("ssh://example.net:2048/")?; | ||
/// | ||
/// url.set_port(Some(4096)).map_err(|_| "cannot be base")?; | ||
|
@@ -1736,9 +1785,22 @@ impl Url { | |
/// | ||
/// ```rust | ||
/// use url::Url; | ||
/// # use std::error::Error; | ||
/// # use url::ParseError; | ||
/// # #[derive(Debug)] | ||
/// # /// A simple wrapper error struct for `no_std` support | ||
/// # struct TestError; | ||
/// # impl From<ParseError> for TestError { | ||
/// # fn from(value: ParseError) -> Self { | ||
/// # TestError {} | ||
/// # } | ||
/// # } | ||
/// # impl From<&str> for TestError { | ||
/// # fn from(value: &str) -> Self { | ||
/// # TestError {} | ||
/// # } | ||
/// # } | ||
/// | ||
/// # fn run() -> Result<(), Box<dyn Error>> { | ||
/// # fn run() -> Result<(), TestError> { | ||
/// let mut url = Url::parse("https://example.org/")?; | ||
/// | ||
/// url.set_port(Some(443)).map_err(|_| "cannot be base")?; | ||
|
@@ -2419,7 +2481,12 @@ impl Url { | |
/// # run().unwrap(); | ||
/// # } | ||
/// ``` | ||
#[cfg(any(unix, windows, target_os = "redox", target_os = "wasi"))] | ||
/// | ||
/// This method is only available if the `std` Cargo feature is enabled. | ||
#[cfg(all( | ||
feature = "std", | ||
any(unix, windows, target_os = "redox", target_os = "wasi") | ||
))] | ||
#[allow(clippy::result_unit_err)] | ||
pub fn from_file_path<P: AsRef<Path>>(path: P) -> Result<Url, ()> { | ||
let mut serialization = "file://".to_owned(); | ||
|
@@ -2456,7 +2523,12 @@ impl Url { | |
/// | ||
/// Note that `std::path` does not consider trailing slashes significant | ||
/// and usually does not include them (e.g. in `Path::parent()`). | ||
#[cfg(any(unix, windows, target_os = "redox", target_os = "wasi"))] | ||
/// | ||
/// This method is only available if the `std` Cargo feature is enabled. | ||
#[cfg(all( | ||
feature = "std", | ||
any(unix, windows, target_os = "redox", target_os = "wasi") | ||
))] | ||
#[allow(clippy::result_unit_err)] | ||
pub fn from_directory_path<P: AsRef<Path>>(path: P) -> Result<Url, ()> { | ||
let mut url = Url::from_file_path(path)?; | ||
|
@@ -2572,8 +2644,13 @@ impl Url { | |
/// or if `Path::new_opt()` returns `None`. | ||
/// (That is, if the percent-decoded path contains a NUL byte or, | ||
/// for a Windows path, is not UTF-8.) | ||
/// | ||
/// This method is only available if the `std` Cargo feature is enabled. | ||
#[inline] | ||
#[cfg(any(unix, windows, target_os = "redox", target_os = "wasi"))] | ||
#[cfg(all( | ||
feature = "std", | ||
any(unix, windows, target_os = "redox", target_os = "wasi") | ||
))] | ||
#[allow(clippy::result_unit_err)] | ||
pub fn to_file_path(&self) -> Result<PathBuf, ()> { | ||
if let Some(segments) = self.path_segments() { | ||
|
@@ -2777,11 +2854,13 @@ impl<'de> serde::Deserialize<'de> for Url { | |
} | ||
} | ||
|
||
#[cfg(any(unix, target_os = "redox", target_os = "wasi"))] | ||
#[cfg(all(feature = "std", any(unix, target_os = "redox", target_os = "wasi")))] | ||
fn path_to_file_url_segments( | ||
path: &Path, | ||
serialization: &mut String, | ||
) -> Result<(u32, HostInternal), ()> { | ||
use crate::parser::PATH_SEGMENT; | ||
use percent_encoding::percent_encode; | ||
#[cfg(any(unix, target_os = "redox"))] | ||
use std::os::unix::prelude::OsStrExt; | ||
#[cfg(target_os = "wasi")] | ||
|
@@ -2807,20 +2886,23 @@ fn path_to_file_url_segments( | |
Ok((host_end, HostInternal::None)) | ||
} | ||
|
||
#[cfg(windows)] | ||
#[cfg(all(feature = "std", windows))] | ||
fn path_to_file_url_segments( | ||
path: &Path, | ||
serialization: &mut String, | ||
) -> Result<(u32, HostInternal), ()> { | ||
path_to_file_url_segments_windows(path, serialization) | ||
} | ||
|
||
#[cfg(feature = "std")] | ||
// Build this unconditionally to alleviate https://github.com/servo/rust-url/issues/102 | ||
#[cfg_attr(not(windows), allow(dead_code))] | ||
fn path_to_file_url_segments_windows( | ||
path: &Path, | ||
serialization: &mut String, | ||
) -> Result<(u32, HostInternal), ()> { | ||
use crate::parser::PATH_SEGMENT; | ||
use percent_encoding::percent_encode; | ||
use std::path::{Component, Prefix}; | ||
if !path.is_absolute() { | ||
return Err(()); | ||
|
@@ -2879,11 +2961,12 @@ fn path_to_file_url_segments_windows( | |
Ok((host_end, host_internal)) | ||
} | ||
|
||
#[cfg(any(unix, target_os = "redox", target_os = "wasi"))] | ||
#[cfg(all(feature = "std", any(unix, target_os = "redox", target_os = "wasi")))] | ||
fn file_url_segments_to_pathbuf( | ||
host: Option<&str>, | ||
segments: str::Split<'_, char>, | ||
) -> Result<PathBuf, ()> { | ||
use percent_encoding::percent_decode; | ||
use std::ffi::OsStr; | ||
#[cfg(any(unix, target_os = "redox"))] | ||
use std::os::unix::prelude::OsStrExt; | ||
|
@@ -2924,20 +3007,22 @@ fn file_url_segments_to_pathbuf( | |
Ok(path) | ||
} | ||
|
||
#[cfg(windows)] | ||
#[cfg(all(feature = "std", windows))] | ||
fn file_url_segments_to_pathbuf( | ||
host: Option<&str>, | ||
segments: str::Split<char>, | ||
) -> Result<PathBuf, ()> { | ||
file_url_segments_to_pathbuf_windows(host, segments) | ||
} | ||
|
||
#[cfg(feature = "std")] | ||
// Build this unconditionally to alleviate https://github.com/servo/rust-url/issues/102 | ||
#[cfg_attr(not(windows), allow(dead_code))] | ||
fn file_url_segments_to_pathbuf_windows( | ||
host: Option<&str>, | ||
mut segments: str::Split<'_, char>, | ||
) -> Result<PathBuf, ()> { | ||
use percent_encoding::percent_decode; | ||
let mut string = if let Some(host) = host { | ||
r"\\".to_owned() + host | ||
} else { | ||
|
There was a problem hiding this comment.
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?There was a problem hiding this comment.
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 testno_std
compatibility is to:std
(certain ARM platforms) ORno_std
binary crate which uses the librariesThere was a problem hiding this comment.
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
There was a problem hiding this comment.
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?