Trying to use path-absolutize crate

I'm adapting my file system abstraction layer to:

  • Absolutize path. That is, something like "/foo/../bar.bin" turns into "/bar.bin"
  • Absolutize path under a certain directory. For example, "/appdata/../.." turns into "/appdata" (cannot go out of "/appdata").

I found the path-absolutize might work for this purpose. When constructing a File object, either some_std_path.absolutize() or some_std_path.absolutize_virtually() is used:

impl File {
    /// Constructs a new `File` object.
    pub fn new<T: AsRef<str>>(url_or_path: T) -> Self {
        let url_or_path = url_or_path.as_ref();
        if url_or_path.starts_with("file:") {
            File { m_scheme: FileScheme::File, m_path: String::from(Path::new(&url_or_path[5..]).absolutize().unwrap().to_str().unwrap_or("")) }
        } else if url_or_path.starts_with("app:") {
            let p = Path::new(&url_or_path[4..])
                .absolutize_virtually::<&str>(
                    unsafe { APPLICATION_DIRECTORY }.unwrap().as_ref())
                .unwrap().to_str().unwrap_or(unsafe {APPLICATION_DIRECTORY}.unwrap().as_ref());
            File { m_scheme: FileScheme::App, m_path: String::from(p) }
        } else if url_or_path.starts_with("app-storage:") {
            let p = Path::new(&url_or_path[12..])
                .absolutize_virtually::<&str>(
                    unsafe { APPLICATION_STORAGE_DIRECTORY }.unwrap().as_ref())
                .unwrap().to_str().unwrap_or(unsafe {APPLICATION_STORAGE_DIRECTORY}.unwrap().as_ref());
            File { m_scheme: FileScheme::AppStorage, m_path: String::from(p) }
        } else {
            File { m_scheme: FileScheme::File, m_path: String::from(Path::new(url_or_path).absolutize().unwrap().to_str().unwrap_or("")) }
        }
    }
    // ...
}

lib.rs#L39

I'm getting this:

app: scheme

temporary value dropped while borrowed
consider using a `let` binding to create a longer lived valuerustcE0716
lib.rs(50, 95): temporary value is freed at the end of this statement
lib.rs(51, 68): borrow later used here


app-storage: scheme

temporary value dropped while borrowed
consider using a `let` binding to create a longer lived valuerustcE0716
lib.rs(56, 103): temporary value is freed at the end of this statement
lib.rs(57, 75): borrow later used here

I'm noob with Cow, I had to call cow.into_owned. IDE stopped complaining:

let app_dir = unsafe { APPLICATION_DIRECTORY.clone().unwrap() };
let p = Path::new(&url_or_path[4..]).absolutize_virtually(app_dir.clone()).unwrap_or(std::borrow::Cow::Owned(PathBuf::from(app_dir.clone()))).into_owned();
File { m_scheme: FileScheme::App, m_path: String::from(p.to_str().unwrap_or(app_dir.as_ref())) }

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.