Skip to content

Commit 8c5f81c

Browse files
committed
remove lossy conversions, fix linting and doc tests, add more tests for Path replacement methods
1 parent 37473a3 commit 8c5f81c

File tree

3 files changed

+37
-5
lines changed

3 files changed

+37
-5
lines changed

crates/bevy_asset/src/io/embedded/embedded_watcher.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::io::{
33
memory::Dir,
44
AssetSourceEvent, AssetWatcher,
55
};
6-
use alloc::{boxed::Box, sync::Arc, vec::Vec};
6+
use alloc::{boxed::Box, string::ToString, sync::Arc, vec::Vec};
77
use bevy_platform::collections::HashMap;
88
use core::time::Duration;
99
use notify_debouncer_full::{notify::RecommendedWatcher, Debouncer, RecommendedCache};
@@ -40,7 +40,9 @@ impl EmbeddedWatcher {
4040
last_event: None,
4141
};
4242
let watcher = new_asset_event_debouncer(
43-
root.to_string_lossy().to_string(),
43+
root.to_str()
44+
.expect("non unicode characters found in path")
45+
.to_string(),
4446
debounce_wait_time,
4547
handler,
4648
)

crates/bevy_asset/src/io/source.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,9 @@ impl AssetSource {
545545
if path.exists() {
546546
Some(Box::new(
547547
super::file::FileWatcher::new(
548-
path.to_string_lossy().to_string(),
548+
path.to_str()
549+
.expect("non unicode characters found in path")
550+
.to_string(),
549551
sender,
550552
file_debounce_wait_time,
551553
)

crates/bevy_asset/src/path.rs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,11 @@ impl<'a> From<&'a Path> for AssetPath<'a> {
628628
fn from(path: &'a Path) -> Self {
629629
Self {
630630
source: AssetSourceId::Default,
631-
path: CowArc::from(path.to_string_lossy().to_string()),
631+
path: CowArc::from(
632+
path.to_str()
633+
.expect("non unicode characters found in file path")
634+
.to_string(),
635+
),
632636
label: None,
633637
}
634638
}
@@ -639,7 +643,11 @@ impl From<PathBuf> for AssetPath<'static> {
639643
fn from(path: PathBuf) -> Self {
640644
Self {
641645
source: AssetSourceId::Default,
642-
path: CowArc::from(path.to_string_lossy().to_string()),
646+
path: CowArc::from(
647+
path.to_str()
648+
.expect("non unicode characters found in file path")
649+
.to_string(),
650+
),
643651
label: None,
644652
}
645653
}
@@ -1068,4 +1076,24 @@ mod tests {
10681076
assert_eq!(AssetPath::from("a/b/"), AssetPath::from("a/b"));
10691077
assert_eq!(AssetPath::from("a/b/#c"), AssetPath::from("a/b#c"));
10701078
}
1079+
1080+
#[test]
1081+
fn test_path_components() {
1082+
use alloc::vec;
1083+
use alloc::vec::Vec;
1084+
1085+
assert_eq!(
1086+
AssetPath::from("a/b/c")
1087+
.path_components()
1088+
.collect::<Vec<_>>(),
1089+
vec!["a", "b", "c"]
1090+
);
1091+
1092+
assert_eq!(
1093+
AssetPath::from("a/b/c/../d")
1094+
.path_components()
1095+
.collect::<Vec<_>>(),
1096+
vec!["a", "b", "c", "..", "d"]
1097+
);
1098+
}
10711099
}

0 commit comments

Comments
 (0)