Skip to content

Commit

Permalink
fix: support wasm32-wasip2 on stable channel (#983)
Browse files Browse the repository at this point in the history
Signed-off-by: Brooks Townsend <[email protected]>
  • Loading branch information
brooksmtownsend authored Oct 28, 2024
1 parent bf089c4 commit 30e6258
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 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 @@ -2985,8 +2982,6 @@ fn path_to_file_url_segments(
use std::os::hermit::ffi::OsStrExt;
#[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 @@ -2996,10 +2991,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 @@ -3093,13 +3094,12 @@ fn file_url_segments_to_pathbuf(
) -> Result<PathBuf, ()> {
use alloc::vec::Vec;
use percent_encoding::percent_decode;
#[cfg(not(target_os = "wasi"))]
use std::ffi::OsStr;
#[cfg(target_os = "hermit")]
use std::os::hermit::ffi::OsStrExt;
#[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 @@ -3125,8 +3125,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 30e6258

Please sign in to comment.