vinezombie 0.3.1

A modular IRCv3 framework
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
use super::{Transform, Utf8Policy};
use std::{
    borrow::Cow,
    ops::Deref,
    sync::atomic::{AtomicI8, Ordering::Relaxed},
};

/// Placeholder string for when some value cannot be displayed,
/// usually due to either being a non-UTF-8 string or secret.
pub const DISPLAY_PLACEHOLDER: &str = "<?>";

/// A borrowing or shared-owning immutable byte string.
///
/// See the [module-level documentation][super] for more.
#[derive(Default)]
pub struct Bytes<'a> {
    value: &'a [u8],
    /// If this is Some, `value` points to data owned by this.
    /// It's very important that the slice never be returned with
    /// a lifetime longer than the IrcStr it was obvained from.
    ownership: Option<OwnedBytes>,
    /// The result of UTF-8 validity checks.
    /// 0 if "unknown", 1 if UTF-8, -1 if NOT UTF-8.
    utf8: AtomicI8,
    /// Whether this string is "secret" or not.
    secret: bool,
}

impl Bytes<'static> {
    /// Returns a [`Cow`] `str` with `'static` lifetime containing `self`'s value as a UTF-8 string
    /// with any non-UTF-8 byte sequences replaced with the
    /// [U+FFFD replacement character](std::char::REPLACEMENT_CHARACTER).
    ///
    /// This is efficient for `Bytes` instances constructed out of string literals.
    pub fn to_utf8_lossy_static(&self) -> Cow<'static, str> {
        if self.is_owning() {
            Cow::Owned(self.to_utf8_lossy().into_owned())
        } else {
            unsafe { self.utf8_cow() }
        }
    }
}

unsafe impl<'a> crate::owning::MakeOwning for crate::string::Bytes<'a> {
    type This<'b> = crate::string::Bytes<'b>;

    fn make_owning(&mut self) {
        if !self.is_owning() {
            self.owning_force(false);
        }
    }
}

impl<'a> Bytes<'a> {
    /// Returns a new empty `Bytes`.
    pub const fn empty() -> Bytes<'a> {
        Bytes { value: &[], ownership: None, utf8: AtomicI8::new(1), secret: false }
    }
    /// Cheaply converts a byte slice into a `Bytes`.
    pub const fn from_bytes(value: &'a [u8]) -> Self {
        Bytes { value, ownership: None, utf8: AtomicI8::new(0), secret: false }
    }
    /// Cheaply converts an `str` into a `Bytes`.
    pub const fn from_str(value: &'a str) -> Self {
        Bytes { value: value.as_bytes(), ownership: None, utf8: AtomicI8::new(1), secret: false }
    }
    /// Cheaply conversts a secret value into a `Bytes`.
    pub fn from_secret(value: Vec<u8>) -> Self {
        let (ownership, value) = unsafe { OwnedBytes::from_vec(value) };
        Bytes { value, ownership, utf8: AtomicI8::new(0), secret: true }
    }
    /// Returns `true` if `self` is not borrowing its data.
    pub const fn is_owning(&self) -> bool {
        self.ownership.is_some()
    }
    /// Returns `true` if `self` is a sensitive byte-string.
    pub fn is_secret(&self) -> bool {
        self.secret
    }
    /// Returns an owning version of this string.
    ///
    /// If this string already owns its data, this method only extends its lifetime.
    pub fn owning<'b>(mut self) -> Bytes<'b> {
        if !self.is_owning() {
            let secret = self.secret;
            self.owning_force(secret);
        }
        // Lifetime extension.
        unsafe { std::mem::transmute(self) }
    }
    /// Returns a secret version of this string.
    ///
    /// Secret strings' contents are not printed in formatting strings,
    /// whether using `Display` or `Debug`.
    /// Clones of secret strings are also secret.
    ///
    /// Owning versions of these strings' buffers are zeroed out
    /// when the last reference to them is lost.
    pub fn secret(mut self) -> Self {
        if !self.secret && self.is_owning() {
            self.owning_force(true);
        } else {
            self.secret = true;
        }
        self
    }
    fn owning_force(&mut self, secret: bool) {
        unsafe {
            let (ownership, value) = OwnedBytes::from_vec(self.value.to_vec());
            *self = Bytes { value, ownership, utf8: self.utf8.load(Relaxed).into(), secret }
        }
    }

    /// Returns true if this byte string is empty.
    pub const fn is_empty(&self) -> bool {
        self.value.is_empty()
    }
    /// Returns the length of this byte string.
    pub const fn len(&self) -> usize {
        self.value.len()
    }
    /// Checks if `self` is known to be UTF-8 without checking the whole string.
    ///
    /// This operation does not perform UTF-8 validity checks.
    #[inline]
    pub fn is_utf8_lazy(&self) -> Option<bool> {
        match self.utf8.load(Relaxed) {
            1 => Some(true),
            -1 => Some(false),
            _ => None,
        }
    }
    /// Returns a reference to `self`'s value as a UTF-8 string if it's correctly encoded.
    ///
    /// This operation may do a UTF-8 validity check.
    /// If `self` was constructed from a UTF-8 string
    /// or a UTF-8 check was done previously, this check will be skipped.
    pub fn to_utf8(&self) -> Option<&str> {
        match self.is_utf8_lazy() {
            Some(true) => Some(unsafe { std::str::from_utf8_unchecked(self.value) }),
            Some(false) => None,
            None => {
                let so = std::str::from_utf8(self.value).ok();
                let utf8 = if so.is_some() { 1i8 } else { -1i8 };
                self.utf8.store(utf8, Relaxed);
                so
            }
        }
    }
    /// Returns a [`Cow`] `str` containing `self`'s value as a UTF-8 string
    /// with any non-UTF-8 byte sequences replaced with the
    /// [U+FFFD replacement character](std::char::REPLACEMENT_CHARACTER).
    pub fn to_utf8_lossy(&self) -> Cow<'_, str> {
        unsafe { self.utf8_cow() }
    }
    /// Returns `self` as a UTF-8 string,
    /// replacing any non-UTF-8 byte sequences with the the
    /// [U+FFFD replacement character](std::char::REPLACEMENT_CHARACTER).
    pub fn into_utf8_lossy(self) -> Self {
        match unsafe { self.utf8_cow() } {
            Cow::Borrowed(s) => Bytes {
                value: s.as_bytes(),
                ownership: self.ownership,
                utf8: 1i8.into(),
                secret: self.secret,
            },
            Cow::Owned(o) => o.into(),
        }
    }
    #[cfg(feature = "base64")]
    fn to_base64_impl(&self) -> Bytes<'static> {
        use base64::engine::{general_purpose::STANDARD as ENGINE, Engine};
        let encoded = ENGINE.encode(self.value);
        unsafe {
            let (ownership, value) = OwnedBytes::from_vec(encoded.into_bytes());
            Bytes { value, ownership, utf8: 1i8.into(), secret: self.secret }
        }
    }
    /// Creates a base64-encoded version of this string.
    ///
    /// The returned string is always owning if it's non-empty.
    /// If `self` is secret, the returned string will also be secret.
    #[cfg(feature = "base64")]
    pub fn to_base64(&self) -> super::Word<'static> {
        unsafe { super::Word::from_unchecked(self.to_base64_impl()) }
    }
    /// Creates a base64-encoded version of this string,
    /// using the literal `"+"` if `self` is empty.
    ///
    /// The returned string is always owning if `self` is non-empty.
    /// If `self` is secret, the returned string will also be secret.
    #[cfg(feature = "base64")]
    pub fn to_base64_plus(&self) -> super::Arg<'static> {
        if self.is_empty() {
            crate::names::PLUS
        } else {
            unsafe { super::Arg::from_unchecked(self.to_base64_impl()) }
        }
    }
    unsafe fn utf8_cow(&self) -> Cow<'a, str> {
        match self.is_utf8_lazy() {
            Some(true) => Cow::Borrowed(unsafe { std::str::from_utf8_unchecked(self.value) }),
            Some(false) => String::from_utf8_lossy(self.value),
            None => {
                let sl = String::from_utf8_lossy(self.value);
                let utf8 = if matches!(&sl, Cow::Borrowed(_)) { 1i8 } else { -1i8 };
                self.utf8.store(utf8, Relaxed);
                sl
            }
        }
    }
    fn utf8_for_policy(&self, utf8: Utf8Policy) -> i8 {
        match utf8 {
            Utf8Policy::PreserveStrict => self.utf8.load(Relaxed),
            Utf8Policy::Preserve => (self.utf8.load(Relaxed) == 1) as i8,
            Utf8Policy::Invalid | Utf8Policy::Recheck | Utf8Policy::Valid => utf8 as i8,
        }
    }
    /// Returns `self`'s value as a slice.
    pub const fn as_bytes(&self) -> &[u8] {
        self.value
    }
    /// Returns `self`'s value as a slice with lifetime `'a`.
    ///
    /// # Safety
    /// If `self [is owning][Bytes::is_owning], then
    /// the returned slice must not outlive `self`.
    pub const unsafe fn as_bytes_unsafe(&self) -> &'a [u8] {
        self.value
    }
    /// Creates a clone of `self` using `value` as the value.
    ///
    /// # Safety
    /// If `self [is owning][Bytes::is_owning], then
    /// `value` must either point to data owned by `self` or an immutable static variable.
    /// A valid value can be obtained using [`as_bytes_unsafe`][Bytes::as_bytes_unsafe].
    ///
    /// The `utf8` parameter is trusted to be correct,
    /// and byte slices may be incorrectly cast unchecked to `str`s otherwise.
    pub unsafe fn using_value(&self, value: &'a [u8], utf8: Utf8Policy) -> Self {
        let utf8 = self.utf8_for_policy(utf8);
        let ownership = if value.is_empty() { None } else { self.ownership.clone() };
        Bytes { value, ownership, utf8: utf8.into(), secret: self.secret }
    }
    /// Updates `self` using the provided [`Transform`].
    pub fn transform<T: Transform>(&mut self, tf: T) -> T::Value {
        let tfed = tf.transform(self);
        if tfed.transformed.as_ref().is_empty() {
            *self = Bytes::empty();
            return tfed.value;
        }
        match tfed.transformed {
            Cow::Borrowed(s) => {
                match tfed.utf8 {
                    Utf8Policy::PreserveStrict => (),
                    Utf8Policy::Preserve => {
                        let _ = self.utf8.compare_exchange(-1i8, 0i8, Relaxed, Relaxed);
                    }
                    Utf8Policy::Invalid | Utf8Policy::Recheck | Utf8Policy::Valid => {
                        self.utf8.store(tfed.utf8 as i8, Relaxed);
                    }
                }
                self.value = s;
            }
            Cow::Owned(o) => {
                let utf8 = self.utf8_for_policy(tfed.utf8);
                unsafe {
                    let (ownership, value) = OwnedBytes::from_vec(o);
                    *self = Bytes { value, ownership, utf8: utf8.into(), secret: self.secret };
                }
            }
        }
        tfed.value
    }
    /// Transforms `self` into a [`Vec`].
    ///
    /// This operation copies data unless `self` has sole ownership of its data.
    pub fn into_vec(self) -> Vec<u8> {
        if let Some(owner) = self.ownership {
            unsafe { owner.into_vec(self.value, self.secret) }
        } else {
            self.value.to_vec()
        }
    }
}

impl<'a> Deref for Bytes<'a> {
    type Target = [u8];

    fn deref(&self) -> &Self::Target {
        self.value
    }
}

impl AsRef<[u8]> for Bytes<'_> {
    fn as_ref(&self) -> &[u8] {
        self.value
    }
}

impl std::borrow::Borrow<[u8]> for Bytes<'_> {
    fn borrow(&self) -> &[u8] {
        self.value
    }
}

// Conversions to IrcStr.

impl<'a> From<Vec<u8>> for Bytes<'a> {
    fn from(value: Vec<u8>) -> Self {
        unsafe {
            let (ownership, value) = OwnedBytes::from_vec(value);
            Bytes { value, ownership, utf8: 0i8.into(), secret: false }
        }
    }
}

impl<'a> From<String> for Bytes<'a> {
    fn from(value: String) -> Self {
        unsafe {
            let (ownership, value) = OwnedBytes::from_vec(value.into_bytes());
            Bytes { value, ownership, utf8: 1i8.into(), secret: false }
        }
    }
}

impl<'a> From<&'a [u8]> for Bytes<'a> {
    fn from(value: &'a [u8]) -> Self {
        value.to_vec().into()
    }
}

impl<'a> From<&'a str> for Bytes<'a> {
    fn from(value: &'a str) -> Self {
        value.to_owned().into()
    }
}

impl<'a> From<Cow<'a, [u8]>> for Bytes<'a> {
    fn from(value: Cow<'a, [u8]>) -> Self {
        match value {
            Cow::Borrowed(s) => s.into(),
            Cow::Owned(s) => s.into(),
        }
    }
}

impl<'a> From<Cow<'a, str>> for Bytes<'a> {
    fn from(value: Cow<'a, str>) -> Self {
        match value {
            Cow::Borrowed(s) => s.into(),
            Cow::Owned(s) => s.into(),
        }
    }
}

impl<'a> From<Bytes<'a>> for Vec<u8> {
    fn from(value: Bytes<'a>) -> Self {
        value.into_vec()
    }
}

impl<'a> From<Bytes<'a>> for Cow<'a, [u8]> {
    fn from(value: Bytes<'a>) -> Self {
        match value.ownership {
            Some(v) => Cow::Owned(unsafe { v.into_vec(value.value, value.secret) }),
            None => Cow::Borrowed(value.value),
        }
    }
}

// Other impls.

impl Clone for Bytes<'_> {
    fn clone(&self) -> Self {
        Bytes {
            value: self.value,
            ownership: self.ownership.clone(),
            utf8: self.utf8.load(Relaxed).into(),
            secret: self.secret,
        }
    }
}

impl PartialEq for Bytes<'_> {
    fn eq(&self, b: &Bytes<'_>) -> bool {
        self.value == b.value
    }
}
impl<const N: usize> PartialEq<[u8; N]> for Bytes<'_> {
    fn eq(&self, other: &[u8; N]) -> bool {
        self.value == other.as_slice()
    }
}
impl<const N: usize> PartialEq<&[u8; N]> for Bytes<'_> {
    fn eq(&self, other: &&[u8; N]) -> bool {
        self == *other
    }
}
impl PartialEq<[u8]> for Bytes<'_> {
    fn eq(&self, other: &[u8]) -> bool {
        self.value == other
    }
}
impl PartialEq<&[u8]> for Bytes<'_> {
    fn eq(&self, other: &&[u8]) -> bool {
        self == *other
    }
}
impl PartialEq<str> for Bytes<'_> {
    fn eq(&self, other: &str) -> bool {
        // A proper UTF-8 validity check might be pointless here in many cases.
        self.utf8.load(Relaxed) != -1 && self.value == other.as_bytes()
    }
}
impl PartialEq<&str> for Bytes<'_> {
    fn eq(&self, other: &&str) -> bool {
        self == *other
    }
}

impl Eq for Bytes<'_> {}

impl PartialOrd for Bytes<'_> {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for Bytes<'_> {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        self.value.cmp(other.value)
    }
}

impl std::hash::Hash for Bytes<'_> {
    fn hash<H: std::hash::Hasher>(&self, hasher: &mut H) {
        self.value.hash(hasher);
    }
}

impl std::fmt::Display for Bytes<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        if self.is_secret() {
            f.write_str(DISPLAY_PLACEHOLDER)
        } else if let Some(s) = self.to_utf8() {
            f.write_str(s)
        } else {
            f.write_str(DISPLAY_PLACEHOLDER)
        }
    }
}

impl std::fmt::Debug for Bytes<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let mut f = f.debug_struct("Bytes");
        let f = f.field("owning", &self.is_owning());
        if !self.is_secret() {
            if let Some(v) = self.to_utf8() {
                f.field("value", &v)
            } else {
                f.field("value", &self.value)
            }
        } else {
            f.field("value", &DISPLAY_PLACEHOLDER)
        }
        .finish()
    }
}

/// Implementation detail of Bytes.
/// A reference-counted dynamically-sized byte array.
///
/// It is designed to allow dirt-cheap conversions from Vec.
/// It returns a reference to the content on construction
/// but otherwise provides no access to the content.
/// There is also only a strong reference count, which is kept seperate.
#[repr(transparent)]
#[derive(Clone)]
struct OwnedBytes(crate::util::ThinArc<crate::util::OwnedSlice<u8>>);

impl OwnedBytes {
    /// Converts a Vec into Self (which owns the data)
    /// and a slice with an unbound lifetime (which does not).
    ///
    /// Returns `None` and an empty slice if `value` is empty.
    ///
    /// # Safety
    /// This returns a reference with an unbound lifetime.
    /// Make sure this lifetime doesn't leak anywhere that might outlive
    /// the `Self`.
    pub unsafe fn from_vec<'a>(value: Vec<u8>) -> (Option<Self>, &'a [u8]) {
        if value.is_empty() {
            return (None, &[]);
        }
        let (os, len) = crate::util::OwnedSlice::from_vec(value);
        let slice = unsafe { os.as_slice(len) };
        (Some(OwnedBytes(crate::util::ThinArc::new(os))), slice)
    }
    /// Attempts to re-use the buffer for constructing a `Vec` from `slice`.
    ///
    /// # Safety
    /// `slice` is assumed to be a slice of the data owned by self.
    pub unsafe fn into_vec(self, slice: &[u8], secret: bool) -> Vec<u8> {
        if let Ok(os) = self.0.try_unwrap() {
            let (retval, destroy) = os.into_vec_with_slice(slice);
            if secret {
                if let Some(mut destroy) = destroy {
                    destroy.reinit_all();
                }
            }
            retval
        } else {
            slice.to_vec()
        }
    }
}

// Should be send-safe and sync-safe since `data` is never written to after-construction
// unless the atomic ref count indicates exclusive ownership.
unsafe impl Send for OwnedBytes {}
unsafe impl Sync for OwnedBytes {}

// Unfortunately, address sanitizer support in rustc is still unstable.
// https://github.com/rust-lang/rust/issues/39699