Skip to content

Commit

Permalink
fix: support wasm32-wasip2 on stable channel
Browse files Browse the repository at this point in the history
Signed-off-by: Brooks Townsend <brooksmtownsend@gmail.com>
  • Loading branch information
brooksmtownsend committed Oct 18, 2024
1 parent ebd5cfb commit 53d5b0c
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions url/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,6 @@ url = { version = "2", features = ["debugger_visualizer"] }
feature = "debugger_visualizer",
debugger_visualizer(natvis_file = "../../debug_metadata/url.natvis")
)]
// We use std::os::wasi::prelude::OsStrExt, and that is conditionally feature gated
// to be unstable on wasm32-wasip2. https://github.com/rust-lang/rust/issues/130323
#![cfg_attr(all(target_os = "wasi", target_env = "p2"), feature(wasip2))]

pub use form_urlencoded;

Expand Down Expand Up @@ -2892,8 +2889,6 @@ fn path_to_file_url_segments(
use percent_encoding::percent_encode;
#[cfg(any(unix, target_os = "redox"))]
use std::os::unix::prelude::OsStrExt;
#[cfg(target_os = "wasi")]
use std::os::wasi::prelude::OsStrExt;
if !path.is_absolute() {
return Err(());
}
Expand All @@ -2903,10 +2898,16 @@ fn path_to_file_url_segments(
for component in path.components().skip(1) {
empty = false;
serialization.push('/');
#[cfg(not(target_os = "wasi"))]
serialization.extend(percent_encode(
component.as_os_str().as_bytes(),
SPECIAL_PATH_SEGMENT,
));
#[cfg(target_os = "wasi")]
serialization.extend(percent_encode(
component.as_os_str().to_string_lossy().as_bytes(),
SPECIAL_PATH_SEGMENT,
));
}
if empty {
// An URL’s path must not be empty.
Expand Down Expand Up @@ -2997,11 +2998,8 @@ fn file_url_segments_to_pathbuf(
) -> Result<PathBuf, ()> {
use alloc::vec::Vec;
use percent_encoding::percent_decode;
use std::ffi::OsStr;
#[cfg(any(unix, target_os = "redox"))]
use std::os::unix::prelude::OsStrExt;
#[cfg(target_os = "wasi")]
use std::os::wasi::prelude::OsStrExt;
use std::path::PathBuf;

if host.is_some() {
Expand All @@ -3027,8 +3025,12 @@ fn file_url_segments_to_pathbuf(
bytes.push(b'/');
}

let os_str = OsStr::from_bytes(&bytes);
let path = PathBuf::from(os_str);
#[cfg(not(target_os = "wasi"))]
let path = PathBuf::from(OsStr::from_bytes(&bytes));
#[cfg(target_os = "wasi")]
let path = String::from_utf8(bytes)
.map(|path| PathBuf::from(path))
.map_err(|_| ())?;

debug_assert!(
path.is_absolute(),
Expand Down

0 comments on commit 53d5b0c

Please sign in to comment.