Skip to content

Commit bf1419a

Browse files
committed
Make AbsPath::assert work on plain string slices too
1 parent c246135 commit bf1419a

File tree

2 files changed

+22
-25
lines changed

2 files changed

+22
-25
lines changed

crates/paths/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,8 @@ impl AbsPath {
136136
/// # Panics
137137
///
138138
/// Panics if `path` is not absolute.
139-
pub fn assert(path: &Path) -> &AbsPath {
139+
pub fn assert<P: AsRef<Path> + ?Sized>(path: &P) -> &AbsPath {
140+
let path = path.as_ref();
140141
assert!(path.is_absolute());
141142
unsafe { &*(path as *const Path as *const AbsPath) }
142143
}

crates/rust-analyzer/src/config/tree.rs

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -367,39 +367,39 @@ fn parse_toml(
367367

368368
#[cfg(test)]
369369
mod tests {
370-
use std::path::{Path, PathBuf};
370+
use std::path::PathBuf;
371371

372372
use vfs::{AbsPath, AbsPathBuf, VfsPath};
373373

374374
fn alloc_file_id(vfs: &mut Vfs, s: &str) -> FileId {
375-
let abs_path = AbsPathBuf::try_from(PathBuf::new().join(s)).unwrap();
376-
375+
let abs_path = AbsPath::assert(s).to_path_buf();
377376
let vfs_path = VfsPath::from(abs_path);
378377
let file_id = vfs.alloc_file_id(vfs_path);
379378
vfs.set_file_id_contents(file_id, None);
380379
file_id
381380
}
382381

383382
fn alloc_config(vfs: &mut Vfs, s: &str, config: &str) -> FileId {
384-
let abs_path = AbsPathBuf::try_from(PathBuf::new().join(s)).unwrap();
385-
383+
let abs_path = AbsPath::assert(s).to_path_buf();
386384
let vfs_path = VfsPath::from(abs_path);
387385
let file_id = vfs.alloc_file_id(vfs_path);
388386
vfs.set_file_id_contents(file_id, Some(config.to_string().into_bytes()));
389387
file_id
390388
}
391389

390+
const XDG_CONFIG_HOME_RATOML: &'static str =
391+
"/home/username/.config/rust-analyzer/rust-analyzer.toml";
392+
392393
use super::*;
393394
#[test]
394395
fn basic() {
395396
tracing_subscriber::fmt().try_init().ok();
396397
let mut vfs = Vfs::default();
397-
let project_root = AbsPath::assert(Path::new("/root"));
398-
let xdg_config_file_id =
399-
alloc_file_id(&mut vfs, "/home/username/.config/rust-analyzer/rust-analyzer.toml");
398+
let project_root = AbsPath::assert("/root");
399+
let xdg_config_file_id = alloc_file_id(&mut vfs, XDG_CONFIG_HOME_RATOML);
400400
let mut config_tree = ConfigDb::new(xdg_config_file_id, project_root.to_path_buf());
401401

402-
let source_roots = ["/root/crate_a"].map(Path::new).map(AbsPath::assert);
402+
let source_roots = ["/root/crate_a"].map(AbsPath::assert);
403403

404404
let _root = alloc_config(
405405
&mut vfs,
@@ -501,13 +501,11 @@ mod tests {
501501
tracing_subscriber::fmt().try_init().ok();
502502
let mut vfs = Vfs::default();
503503

504-
let project_root = AbsPath::assert(Path::new("/root"));
505-
let xdg =
506-
alloc_file_id(&mut vfs, "/home/username/.config/rust-analyzer/rust-analyzer.toml");
504+
let project_root = AbsPath::assert("/root");
505+
let xdg = alloc_file_id(&mut vfs, XDG_CONFIG_HOME_RATOML);
507506
let mut config_tree = ConfigDb::new(xdg, project_root.to_path_buf());
508507

509-
let source_roots =
510-
["/root/crate_a", "/root/crate_a/crate_b"].map(Path::new).map(AbsPath::assert);
508+
let source_roots = ["/root/crate_a", "/root/crate_a/crate_b"].map(AbsPath::assert);
511509
let [crate_a, crate_b] = source_roots
512510
.map(|dir| dir.join("rust-analyzer.toml"))
513511
.map(|path| vfs.alloc_file_id(path.into()));
@@ -539,7 +537,7 @@ mod tests {
539537
// ----
540538

541539
// Now move crate b to the root. This gives a new FileId for crate_b/ra.toml.
542-
let source_roots = ["/root/crate_a", "/root/crate_b"].map(Path::new).map(AbsPath::assert);
540+
let source_roots = ["/root/crate_a", "/root/crate_b"].map(AbsPath::assert);
543541
let [_crate_a, crate_b] = source_roots
544542
.map(|dir| dir.join("rust-analyzer.toml"))
545543
.map(|path| vfs.alloc_file_id(path.into()));
@@ -568,12 +566,11 @@ mod tests {
568566
tracing_subscriber::fmt().try_init().ok();
569567
let mut vfs = Vfs::default();
570568

571-
let project_root = AbsPath::assert(Path::new("/root"));
572-
let xdg =
573-
alloc_file_id(&mut vfs, "/home/username/.config/rust-analyzer/rust-analyzer.toml");
569+
let project_root = AbsPath::assert("/root");
570+
let xdg = alloc_file_id(&mut vfs, XDG_CONFIG_HOME_RATOML);
574571
let mut config_tree = ConfigDb::new(xdg, project_root.to_path_buf());
575572

576-
let source_roots = ["/root/crate_a"].map(Path::new).map(AbsPath::assert);
573+
let source_roots = ["/root/crate_a"].map(AbsPath::assert);
577574
let crate_a = vfs.alloc_file_id(source_roots[0].join("rust-analyzer.toml").into());
578575

579576
let _root = alloc_config(
@@ -601,7 +598,7 @@ mod tests {
601598
// change project root
602599
let changes = ConfigChanges {
603600
client_change: None,
604-
set_project_root: Some(AbsPath::assert(Path::new("/ro")).to_path_buf()),
601+
set_project_root: Some(AbsPath::assert("/ro").to_path_buf()),
605602
set_source_roots: None,
606603
ra_toml_changes: dbg!(vfs.take_changes()),
607604
};
@@ -617,12 +614,11 @@ mod tests {
617614
tracing_subscriber::fmt().try_init().ok();
618615
let mut vfs = Vfs::default();
619616

620-
let project_root = AbsPath::assert(Path::new("/root"));
621-
let xdg =
622-
alloc_file_id(&mut vfs, "/home/username/.config/rust-analyzer/rust-analyzer.toml");
617+
let project_root = AbsPath::assert("/root");
618+
let xdg = alloc_file_id(&mut vfs, XDG_CONFIG_HOME_RATOML);
623619
let mut config_tree = ConfigDb::new(xdg, project_root.to_path_buf());
624620

625-
let source_roots = ["/root/crate_a"].map(Path::new).map(AbsPath::assert);
621+
let source_roots = ["/root/crate_a"].map(AbsPath::assert);
626622
let crate_a = vfs.alloc_file_id(source_roots[0].join("rust-analyzer.toml").into());
627623

628624
let _root = alloc_config(

0 commit comments

Comments
 (0)