file_storage/path/mutate/
truncate.rs

1use crate::StoragePath;
2
3impl StoragePath {
4    //! Truncate
5
6    /// Truncates the path to the `new_len`.
7    ///
8    /// # Safety
9    /// The `new_len` must be >= to the `base_len` and must be a valid char boundary.
10    pub unsafe fn truncate(&mut self, new_len: usize) {
11        debug_assert!(new_len >= self.base_len());
12        debug_assert!(self.path().is_char_boundary(new_len));
13
14        unsafe { self.path_mut() }.truncate(new_len)
15    }
16
17    /// Truncates the path to the `new_len`.
18    ///
19    /// # Safety
20    /// The `new_len` must be >= to the `base_len` and must be a valid char boundary.
21    pub unsafe fn truncated(mut self, new_len: usize) -> Self {
22        debug_assert!(new_len >= self.base_len());
23        debug_assert!(self.path().is_char_boundary(new_len));
24
25        self.truncate(new_len);
26        self
27    }
28}
29
30#[cfg(test)]
31mod tests {
32    use crate::StoragePath;
33
34    #[test]
35    fn truncate() {
36        let path: StoragePath = StoragePath::unix_root().with_appended("file.txt");
37
38        let path: StoragePath = unsafe { path.truncated(5) };
39        assert_eq!(path.as_str(), "/file");
40
41        let path: StoragePath = unsafe { path.truncated(1) };
42        assert_eq!(path.as_str(), "/");
43    }
44}