cj-path-util 0.4.0

Utilities to make working with Path/PathBuf more productive
Documentation

Utilities to make working with Path/PathBuf more productive

AppendToPath

Append a subpath to a path, while re-using the left hand side as much as possible.

use cj_path_util::path_util::AppendToPath;

let left = PathBuf::from("foo");
// Copy `left`
let whole = (&left).append("bar");
// Consume and re-use memory of `left`
let whole2 = left.append("bar");

FixupPath

Replace the "" path with ".", i.e. always yield a valid path on Unix.

use cj_path_util::unix::fixup_path::FixupPath;

let left = PathBuf::from("foo");
// Copy `left`
let whole = (&left).append("bar");
// Consume and re-use memory of `left`
let whole2 = left.append("bar");

Unstable polyfill

Provides functions that currently are, or have recently been, unstable, like add_extension.

Temporary file utils

These are a bit more composable/controllable utilities for handling temporary files. Currently requires the 'unix' feature. Todo: make portable.

The biggest flexibility is when starting with TempfileOptions, but there are also the temp_file_for and unbuffered_temp_file_for procedures to simplify. For the simplest / lowest level bit, temp_path can be called directly.

The approach taken:

  • The usage is for creating files that are meant to stay; not for files that are used temporarily (question: bad naming chosen, then?). For the latter, paths in /tmp or similar are the right choice, and no permanent paths would be desired. This is not the usage case here.

  • Temporary paths are made by appending a ".tmp~{pid}-{tid} suffix to the target path (the path that the file should end up at once finished), to ensure the temporary file lies on the same file system as the target path.

  • The suffix contains only process and thread id; this works for local usage, but not for shared file systems (this could be changed to include the host name, todo?). Note that no random data is added. The assumption here is that the same thread will never hold two instances of TempPath or Tempfile for the same target path at the same time, since those would overwrite the target path anyway; hence, no random number is necessary to distinguish the two. The advantages of this approach are that the number of different lingering temporary files is a bit more bounded, and that knowing the pid and tid allows to look up log file entries.

  • persist has to be called to finish the process of creating the target file; the drop action will not persist the file, but by default unlink the temporary file.

Features

The crate has one feature: unix, which enables functionality that (currently, and presumably) only works on Unix.