d-major 0.0.0

Traverse directory trees in parallel, using relative entries to minimize allocation and maximize parallelism.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/*
 * Description: Handle null-terminated strings with explicit length measurement.
 *
 * Copyright (C) 2025 d@nny mc² <dmc2@hypnicjerk.ai>
 * SPDX-License-Identifier: LGPL-3.0-or-later
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

//! Handle null-terminated strings with explicit length measurement.

use std::{cmp, ffi, fmt, hash, marker::PhantomData, ptr, slice};


/// Similar to [`ffi::CStr`], except that [`Self::from_ptr()`] does not implicitly perform
/// a [`libc::strlen()`].
///
/// The stdlib docs note that this is the eventual goal for `CStr`, but in this case we want to
/// control where the length measurement is performed, so that we can avoid performing it until we
/// actually want to use the string data.
#[derive(Copy, Clone)]
#[repr(transparent)]
pub struct NullTermStr<'s> {
  start: *const ffi::c_char,
  _ph: PhantomData<&'s ffi::c_char>,
}

impl<'s> NullTermStr<'s> {
  /// Wrap a null-terminated C-style string, *without* performing any length calculation or reading
  /// any bytes from the pointed-to value.
  ///
  /// # Safety
  /// `start` must be non-null, and point to a contiguous region of memory which ends with
  /// a null byte. This is the same requirement as for [`ffi::CStr::from_ptr()`].
  #[inline]
  pub const unsafe fn from_ptr(start: *const ffi::c_char) -> Self {
    Self {
      start,
      _ph: PhantomData,
    }
  }

  /// Return the input that was provided to [`Self::from_ptr()`].
  #[inline]
  pub const fn as_ptr(&self) -> *const ffi::c_char { self.start }

  /// Execute [`libc::strlen()`] to calculate the length of the pointed-to string.
  #[inline]
  pub fn measure(self) -> MeasuredNullTermStr<'s> {
    let n = unsafe { libc::strlen(self.start) };
    unsafe { MeasuredNullTermStr::given_measurement(self, n) }
  }

  /// Return whether this matches either of the always-present directory entries `"."` or `".."`.
  ///
  /// This is performed without any call to `strlen()`, and will not read past the first null byte.
  #[inline]
  pub fn match_dir_entries_unmeasured(&self) -> bool {
    let mut p: *const u8 = self.start.cast();
    match unsafe { p.read() } {
      /* This is a zero-length string (should never happen with directory entries). */
      0 => return false,
      /* This begins with a '.' character, so continue. */
      b'.' => (),
      _ => return false,
    }
    /* We know it's not terminated yet, so we can advance the pointer by 1. */
    p = unsafe { p.add(1) };
    match unsafe { p.read() } {
      /* This was the string ".". */
      0 => return true,
      /* This is ".." so far. */
      b'.' => (),
      _ => return false,
    }
    /* Advance a final time. */
    p = unsafe { p.add(1) };
    match unsafe { p.read() } {
      /* This was the string "..". */
      0 => true,
      _ => false,
    }
  }
}

impl cmp::PartialEq for NullTermStr<'_> {
  fn eq(&self, rhs: &Self) -> bool { ptr::eq(self.start, rhs.start) }
}
impl cmp::Eq for NullTermStr<'_> {}
impl hash::Hash for NullTermStr<'_> {
  fn hash<H>(&self, state: &mut H)
  where H: hash::Hasher {
    ptr::hash(self.start, state);
  }
}

impl fmt::Debug for NullTermStr<'_> {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    let s: &ffi::CStr = self.measure().into();
    f.debug_tuple("NullTermStr")
      .field(&self.start)
      .field(&s)
      .finish()
  }
}

/// The more direct analog to [`ffi::CStr`] which explicitly knows the length of its string data.
#[derive(Copy, Clone)]
pub struct MeasuredNullTermStr<'s> {
  s: NullTermStr<'s>,
  n: usize,
}

impl<'s> MeasuredNullTermStr<'s> {
  /// Construct a C-style string with a known length.
  ///
  /// # Safety
  /// The length `n` must correctly describe the number of bytes in the string value pointed to by
  /// `s`, *without* including the trailing null byte. This is to say that `n` may be 0, but the
  /// string pointed to by `s` must be non-null, and must point to a region of memory exactly 1 byte
  /// longer than `n`.
  ///
  /// If the string's length is not known in advance, use [`NullTermStr::measure()`], which
  /// internally calls this method.
  #[inline]
  pub const unsafe fn given_measurement(s: NullTermStr<'s>, n: usize) -> Self { Self { s, n } }

  /// Translate a reference to a byte slice into a measured null-terminated string.
  ///
  /// This method will panic if `s` is empty, if it does not end with a null byte, or if it contains
  /// any internal null bytes.
  #[inline]
  pub fn from_bytes_with_nul(s: &'s [u8]) -> Self {
    assert!(!s.is_empty(), "expected non-empty slice");
    assert_eq!(s[s.len() - 1], 0, "slice must end with null byte");
    assert_eq!(
      memchr::memchr(0, s),
      Some(s.len() - 1),
      "slice must contain no internal null bytes"
    );
    unsafe { Self::from_bytes_with_nul_unchecked(s) }
  }

  /// Translate a reference to a byte slice into a measured null-terminated string.
  ///
  /// # Safety
  /// `s` must be non-empty, must end with a null byte, and may not contain any internal null
  /// bytes. [`Self::from_bytes_with_nul()`] may be used to validate slices.
  #[inline]
  pub const unsafe fn from_bytes_with_nul_unchecked(s: &'s [u8]) -> Self {
    let n = unsafe { s.len().unchecked_sub(1) };
    Self {
      s: unsafe { NullTermStr::from_ptr(s.as_ptr().cast()) },
      n,
    }
  }

  /// Return a slice of the internal string data, *without* trailing null byte.
  #[inline]
  pub const fn as_bytes(&self) -> &'s [u8] {
    unsafe { slice::from_raw_parts(self.as_ptr().cast(), self.len()) }
  }

  /// Translate this data to a platform-specific string, suitable for translation to
  /// [`Path`](std::path::Path).
  #[inline]
  pub fn as_os_str(&self) -> &'s ffi::OsStr {
    use std::os::unix::ffi::OsStrExt;
    ffi::OsStr::from_bytes(self.as_bytes())
  }

  #[inline]
  const fn as_ptr(&self) -> *const ffi::c_char { self.as_unmeasured().as_ptr() }

  /// Length of the internal string data, *without* trailing null byte.
  #[inline]
  pub const fn len(&self) -> usize { self.n }

  /// Whether the internal string data points to a single null byte.
  #[inline]
  pub const fn is_empty(&self) -> bool { self.len() == 0 }

  /// Length of the internal string data, *with* trailing null byte.
  #[inline]
  pub const fn len_with_nul(&self) -> usize { self.n.checked_add(1).unwrap() }

  /// Return a slice of the internal string data, *with* trailing null byte.
  #[inline]
  pub const fn as_bytes_with_nul(&self) -> &'s [u8] {
    unsafe { slice::from_raw_parts(self.as_ptr().cast(), self.len_with_nul()) }
  }

  /// Retrieve the internal string data, which may be converted to a pointer again.
  #[inline]
  pub const fn as_unmeasured(&self) -> NullTermStr<'s> { self.s }

  /// Allocate the necessary space in `v` and copy over the internal string data.
  ///
  /// `v`'s length will be reset to the length of the internal string data, although it will not
  /// reallocate the underlying vector.
  #[inline]
  pub fn clone_into(&self, v: &mut NullTermString) {
    let NullTermString(v) = v;
    v.clear();
    let src = self.as_bytes_with_nul();
    v.reserve(src.len());
    unsafe {
      cfg_if::cfg_if! {
        if #[cfg(feature = "nightly")] {
          v.spare_capacity_mut()[..src.len()].write_copy_of_slice(src);
        } else {
          v.as_mut_ptr()
            .copy_from_nonoverlapping(src.as_ptr(), src.len());
        }
      }
      v.set_len(src.len());
    }
  }
}

impl<'s> From<&'s [u8]> for MeasuredNullTermStr<'s> {
  fn from(s: &'s [u8]) -> Self { Self::from_bytes_with_nul(s) }
}

impl<'s> From<MeasuredNullTermStr<'s>> for &'s [u8] {
  fn from(s: MeasuredNullTermStr<'s>) -> Self { s.as_bytes_with_nul() }
}

impl<'s> From<&'s ffi::CStr> for MeasuredNullTermStr<'s> {
  fn from(s: &'s ffi::CStr) -> Self {
    unsafe { Self::from_bytes_with_nul_unchecked(s.to_bytes_with_nul()) }
  }
}

impl<'s> From<MeasuredNullTermStr<'s>> for &'s ffi::CStr {
  fn from(s: MeasuredNullTermStr<'s>) -> &'s ffi::CStr {
    unsafe { ffi::CStr::from_bytes_with_nul_unchecked(s.as_bytes_with_nul()) }
  }
}

impl cmp::PartialEq for MeasuredNullTermStr<'_> {
  fn eq(&self, rhs: &Self) -> bool { self.as_bytes().eq(rhs.as_bytes()) }
}
impl cmp::Eq for MeasuredNullTermStr<'_> {}
impl cmp::PartialOrd for MeasuredNullTermStr<'_> {
  fn partial_cmp(&self, rhs: &Self) -> Option<cmp::Ordering> { Some(self.cmp(rhs)) }
}
impl cmp::Ord for MeasuredNullTermStr<'_> {
  fn cmp(&self, rhs: &Self) -> cmp::Ordering { self.as_bytes().cmp(rhs.as_bytes()) }
}
impl hash::Hash for MeasuredNullTermStr<'_> {
  fn hash<H>(&self, state: &mut H)
  where H: hash::Hasher {
    self.as_bytes_with_nul().hash(state);
  }
}

impl fmt::Debug for MeasuredNullTermStr<'_> {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    let s: &ffi::CStr = (*self).into();
    f.debug_tuple("MeasuredNullTermStr")
      .field(&self.s.start)
      .field(&self.n)
      .field(&s)
      .finish()
  }
}

/// Owned version of [`MeasuredNullTermStr`].
///
/// Create with [`Self::new()`], then copy over data with [`MeasuredNullTermStr::clone_into()`].
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct NullTermString(Vec<u8>);

impl NullTermString {
  /// Create a new owned string without any data reserved.
  pub const fn new() -> Self { Self(Vec::new()) }

  /// Create a new owned string with at least `n` bytes reserved.
  pub fn with_capacity(n: usize) -> Self { Self(Vec::with_capacity(n)) }
}

impl Default for NullTermString {
  fn default() -> Self { Self::new() }
}

impl From<NullTermString> for ffi::CString {
  fn from(x: NullTermString) -> Self {
    let NullTermString(v) = x;
    unsafe { ffi::CString::from_vec_with_nul_unchecked(v) }
  }
}

impl From<ffi::CString> for NullTermString {
  fn from(x: ffi::CString) -> Self { Self(x.into_bytes_with_nul()) }
}

impl fmt::Debug for NullTermString {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    let s: ffi::CString = self.clone().into();
    f.debug_tuple("NullTermString").field(&s).finish()
  }
}

/// Allow [`NullTermString`] instances to be interpreted as references to [`MeasuredNullTermStr`].
pub trait AsNullTermStr {
  /// Similar to [`AsRef`] or [`Borrow`](std::borrow::Borrow).
  ///
  /// This is necessary because those traits require the use of DSTs (dynamically-sized types) and
  /// cannot be made to work with tagged lifetimes like we use in this module.
  fn as_null_term_str(&self) -> MeasuredNullTermStr<'_>;
}

impl AsNullTermStr for MeasuredNullTermStr<'_> {
  fn as_null_term_str(&self) -> MeasuredNullTermStr<'_> { *self }
}

impl AsNullTermStr for NullTermString {
  fn as_null_term_str(&self) -> MeasuredNullTermStr<'_> {
    unsafe { MeasuredNullTermStr::from_bytes_with_nul_unchecked(&self.0) }
  }
}


#[cfg(test)]
mod test {
  use proptest::{prelude::*, string::bytes_regex};

  use super::*;

  #[test]
  fn matches_dir_entries() {
    let s = NullTermString(vec![b'.', 0]);
    let s = s.as_null_term_str().as_unmeasured();
    assert!(s.match_dir_entries_unmeasured());

    let s = NullTermString(vec![b'.', b'.', 0]);
    let s = s.as_null_term_str().as_unmeasured();
    assert!(s.match_dir_entries_unmeasured());
  }

  proptest! {
    #[test]
    fn not_dir_entries(
      mut s in bytes_regex("(?s-u:[^\x00]*)").unwrap()
        .prop_filter("not '.' or '..'",
                     |v| !(&v[..] == b"." || &v[..] == b".."))
    ) {
      s.push(0);
      let s = NullTermString(s);
      let s = s.as_null_term_str().as_unmeasured();
      prop_assert!(!s.match_dir_entries_unmeasured());
    }

    #[test]
    fn cstring_roundtrip(s in any::<Vec<u8>>()) {
      let n1 = NullTermString(s);
      let c1: ffi::CString = n1.clone().into();
      let n2: NullTermString = c1.into();
      prop_assert_eq!(n1, n2);
    }

    #[test]
    fn nonnull_roundtrip(mut s in bytes_regex("(?s-u:[^\x00]*)").unwrap()) {
      s.push(0);
      let s = NullTermString(s);
      let c: ffi::CString = s.clone().into();
      let c2: &ffi::CStr = s.as_null_term_str().into();
      prop_assert_eq!(c.as_c_str(), c2);
    }

    #[test]
    fn slice_roundtrip(mut s in bytes_regex("(?s-u:[^\x00]*)").unwrap()) {
      s.push(0);
      let s = NullTermString(s);
      let s: MeasuredNullTermStr = s.as_null_term_str();
      let sl: &[u8] = s.into();
      let s2: MeasuredNullTermStr = sl.into();
      prop_assert_eq!(s, s2);
      let sl2: &[u8] = s2.into();
      prop_assert_eq!(sl, sl2);
    }

    #[test]
    fn nonnull_ref(s in bytes_regex("(?s-u:[^\x00]*)").unwrap()) {
      let mut t = s.clone();
      t.push(0);
      let v = NullTermString(t.clone());
      prop_assert_eq!(&t[..], v.as_null_term_str().as_bytes_with_nul());
      prop_assert_eq!(&s[..], v.as_null_term_str().as_bytes());
    }

    #[test]
    fn nonnull_measure(mut s in bytes_regex("(?s-u:[^\x00]*)").unwrap()) {
      s.push(0);
      let s = NullTermString(s);
      let s = s.as_null_term_str();
      prop_assert_eq!(s, s.as_unmeasured().measure());
    }

    #[test]
    fn nonnull_clone_into(mut s in bytes_regex("(?s-u:[^\x00]*)").unwrap()) {
      s.push(0);
      let v = NullTermString(s);
      let s = v.as_null_term_str();
      let mut v2 = NullTermString(Vec::new());
      s.clone_into(&mut v2);
      let s2 = v2.as_null_term_str();
      prop_assert_eq!(s, s2);
      prop_assert_eq!(v, v2);
    }
  }
}