Skip to main content

FileTree

Struct FileTree 

Source
pub struct FileTree { /* private fields */ }
Expand description

FileTree is a specialized data structure for compactly storing in memory hierarchical structure of files and directories. It also provides fast search and access to data.

Implementation Features
If all the file paths you plan to store in FileTree begin with the same long prefix, it’s better to store this prefix separately, outside of this structure.

For example, you have several file paths:

 /very/long/prefix/to/my/files/file.01

 /very/long/prefix/to/my/files/alfa/file.02

 /very/long/prefix/to/my/files/beta/gamma/file.03

Common prefix is: /very/long/prefix/to/my/files - store it separately.

And in FileTree store short paths: /file.01, /alfa/file.02 and /beta/gamma/file.03.

In this case, FileTree will store the following hierarchy:

                      /
        +-------------+--------------+
     file.01         alfa           beta
                      /              /
                   file.02         gamma
                                     /
                                  file.03

All paths in FileTree must be absolute (i.e., start with /).
Do not include any prefixes into paths (for example, like in Windows - C:).

Implementations§

Source§

impl FileTree

Source

pub fn new() -> Self

Creates new file-tree and initialize root as /.

Source

pub fn is_empty(&self) -> bool

Checks if the tree is empty.

Efficiency: O(1)

Source

pub fn contains_file<P: AsRef<Path>>(&self, path: P) -> Result<bool>

Checks if path is contained in the tree as file.

Efficiency: O(n), where n is a path length (in components).

Source

pub fn contains_dir<P: AsRef<Path>>(&self, path: P) -> Result<bool>

Checks if path is contained in the tree as directory.

Efficiency: O(n), where n is a path length (in components).

Source

pub fn add_dir<P: AsRef<Path>>(&mut self, path: P) -> Result<()>

Add directory into tree.

path must be absolute (i.e., start with /) and not contain prefixes (for example, like in Windows - C:).

Efficiency: O(n), where n is a path length (in components).

Source

pub fn add_file<P: AsRef<Path>>(&mut self, path: P) -> Result<()>

Add file into tree.

path must be absolute (i.e., start with /) and not contain prefixes (for example, like in Windows - C:).

Efficiency: O(n), where n is a path length (in components).

Source

pub fn remove_file<P: AsRef<Path>>(&mut self, path: P) -> Result<()>

Removes a file from the tree.

Source

pub fn remove_dir<P: AsRef<Path>>(&mut self, path: P) -> Result<()>

Removes a directory (with all its entries) from the tree.

Source

pub fn clear(&mut self)

Clears all tree contents.

Efficiency: O(1)

Source

pub fn visit(&self, visitor: impl FnMut(&Path))

Visits all leaf elements in the tree and performs a visitor for each of them.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.