rstango 0.3.0

Implementation of the Tango distributed control system
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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
//! Encoding of various common codeset for GIOP.
//! 
//! Specification: https://www.omg.org/spec/CORBA/3.4/Interoperability/PDF
//!                Section 7.10
//! 
//! Specification: https://www.iana.org/assignments/character-sets/character-sets.xhtml

use std::borrow::Cow;
use std::hash::Hash;

use widestring::Utf16Str;

use crate::buf::BufMut;


// ==================== //
//        Traits        //
// ==================== //

/// A single or multi-byte codeset, which strings are encoded to and decoded from. What is
/// appended to a buffer is made room for by the codeset itself.
///
/// # Safety
///
/// [`Self::decode_is_native`] must only be `Some(true)` for valid UTF-8, and
/// [`Self::decode_to_buf`] must only append valid UTF-8: strings are made of what they
/// say without being checked again.
pub unsafe trait CodesetDef {

    /// Encode this string, appending its octets to the buffer, without any terminating
    /// nul, and returning how many they are. Absent if one of its characters has no
    /// encoding in this codeset, the buffer being then left as it was.
    fn encode_to_buf(&self, src: &str, dst: &mut BufMut<u8, 1>) -> Option<usize>;

    /// Whether these octets already are the UTF-8 of the string they encode, which is what
    /// allows a decoded string to borrow them in place. Absent if they are not valid in
    /// this codeset.
    fn decode_is_native(&self, src: &[u8]) -> Option<bool>;

    /// Decode these octets, borrowing them in place when they already are the UTF-8 of
    /// their string, see [`Self::decode_is_native`], and converting them into a newly
    /// allocated string otherwise. Absent if they are not valid in this codeset.
    fn decode_to_cow<'a>(&self, src: &'a [u8]) -> Option<Cow<'a, str>>;

    /// Decode these octets, appending the UTF-8 of their string to the buffer, and
    /// returning how many octets it takes. Absent if they are not valid in this codeset,
    /// the buffer being then left as it was.
    fn decode_to_buf(&self, src: &[u8], dst: &mut BufMut<u8, 1>) -> Option<usize>;

}

/// A wide codeset, of 16-bit code units, which wide strings are encoded to and decoded
/// from. What is appended to a buffer is made room for by the codeset itself.
///
/// # Safety
///
/// [`Self::decode_is_native`] must only be `Some(true)` for valid UTF-16, and
/// [`Self::decode_to_buf`] must only append valid UTF-16: wide strings are made of what
/// they say without being checked again.
pub unsafe trait WideCodesetDef {

    /// Encode this wide string, appending its code units to the buffer, without any
    /// terminating nul, and returning how many they are. Absent if one of its characters
    /// has no encoding in this codeset, the buffer being then left as it was.
    fn encode_to_buf(&self, src: &Utf16Str, dst: &mut BufMut<u16, 1>) -> Option<usize>;

    /// Whether these code units already are the UTF-16 of the wide string they encode,
    /// which is what allows a decoded wide string to borrow them in place. Absent if they
    /// are not valid in this codeset.
    fn decode_is_native(&self, src: &[u16]) -> Option<bool>;

    /// Decode these code units, borrowing them in place when they already are the UTF-16
    /// of their wide string, see [`Self::decode_is_native`], and converting them into a newly
    /// allocated wide string otherwise. Absent if they are not valid in this codeset.
    fn decode_to_cow<'a>(&self, src: &'a [u16]) -> Option<Cow<'a, Utf16Str>>;

    /// Decode these code units, appending the UTF-16 of their wide string to the buffer,
    /// and returning how many code units it takes. Absent if they are not valid in this
    /// codeset, the buffer being then left as it was.
    fn decode_to_buf(&self, src: &[u16], dst: &mut BufMut<u16, 1>) -> Option<usize>;

}

/// Abstract trait for enumeration of natively supported codesets.
pub trait AnyCodeset: Sized + Copy + Eq + Hash + 'static {
    const NATIVE: Self;
    const ALL: &[Self];
    fn id(self) -> u32;
    fn from_id(id: u32) -> Option<Self>;
}

// ==================== //
//       Codesets       //
// ==================== //

/// Generic enumeration that can represent any supported codeset.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Codeset {
    Ascii,
    Utf8,
    Iso8859_1,
    Windows1252,
}

// SAFETY: Each codeset upholds it.
unsafe impl CodesetDef for Codeset {

    fn encode_to_buf(&self, src: &str, dst: &mut BufMut<u8, 1>) -> Option<usize> {
        match self {
            Codeset::Ascii => Ascii.encode_to_buf(src, dst),
            Codeset::Utf8 => Utf8.encode_to_buf(src, dst),
            Codeset::Iso8859_1 => Iso8859_1.encode_to_buf(src, dst),
            Codeset::Windows1252 => Windows1252.encode_to_buf(src, dst),
        }
    }

    fn decode_is_native(&self, src: &[u8]) -> Option<bool> {
        match self {
            Codeset::Ascii => Ascii.decode_is_native(src),
            Codeset::Utf8 => Utf8.decode_is_native(src),
            Codeset::Iso8859_1 => Iso8859_1.decode_is_native(src),
            Codeset::Windows1252 => Windows1252.decode_is_native(src),
        }
    }

    fn decode_to_cow<'a>(&self, src: &'a [u8]) -> Option<Cow<'a, str>> {
        match self {
            Codeset::Ascii => Ascii.decode_to_cow(src),
            Codeset::Utf8 => Utf8.decode_to_cow(src),
            Codeset::Iso8859_1 => Iso8859_1.decode_to_cow(src),
            Codeset::Windows1252 => Windows1252.decode_to_cow(src),
        }
    }

    fn decode_to_buf(&self, src: &[u8], dst: &mut BufMut<u8, 1>) -> Option<usize> {
        match self {
            Codeset::Ascii => Ascii.decode_to_buf(src, dst),
            Codeset::Utf8 => Utf8.decode_to_buf(src, dst),
            Codeset::Iso8859_1 => Iso8859_1.decode_to_buf(src, dst),
            Codeset::Windows1252 => Windows1252.decode_to_buf(src, dst),
        }
    }

}

impl AnyCodeset for Codeset {

    const NATIVE: Self = Codeset::Utf8;
    const ALL: &[Self] = &[
        Self::Ascii,
        Self::Utf8,
        Self::Iso8859_1,
        Self::Windows1252,
    ];

    fn id(self) -> u32 {
        match self {
            Self::Ascii => 0x00010020,
            Self::Utf8 => 0x05010001,
            Self::Iso8859_1 => 0x00010001,
            Self::Windows1252 => 0x100204E4,
        }
    }

    fn from_id(id: u32) -> Option<Self> {
        Some(match id {
            0x00010020 => Self::Ascii,
            0x05010001 => Self::Utf8,
            0x00010001 => Self::Iso8859_1,
            0x100204E4 => Self::Windows1252,
            _ => return None
        })
    }

}

pub struct Utf8;
// SAFETY: Only valid UTF-8 is native, and appended.
unsafe impl CodesetDef for Utf8 {

    fn encode_to_buf(&self, src: &str, dst: &mut BufMut<u8, 1>) -> Option<usize> {
        dst.extend_from_slice(src.as_bytes());
        Some(src.len())
    }

    fn decode_is_native(&self, src: &[u8]) -> Option<bool> {
        std::str::from_utf8(src).is_ok().then_some(true)
    }

    fn decode_to_cow<'a>(&self, src: &'a [u8]) -> Option<Cow<'a, str>> {
        std::str::from_utf8(src).ok().map(Cow::Borrowed)
    }

    fn decode_to_buf(&self, src: &[u8], dst: &mut BufMut<u8, 1>) -> Option<usize> {
        let src = std::str::from_utf8(src).ok()?;
        dst.extend_from_slice(src.as_bytes());
        Some(src.len())
    }

}

pub struct Ascii;
// SAFETY: Only ASCII is native, and appended, which is valid UTF-8.
unsafe impl CodesetDef for Ascii {

    fn encode_to_buf(&self, src: &str, dst: &mut BufMut<u8, 1>) -> Option<usize> {
        if !src.is_ascii() {
            return None;
        }
        dst.extend_from_slice(src.as_bytes());
        Some(src.len())
    }

    fn decode_is_native(&self, src: &[u8]) -> Option<bool> {
        src.is_ascii().then_some(true)
    }

    fn decode_to_cow<'a>(&self, src: &'a [u8]) -> Option<Cow<'a, str>> {
        as_ascii(src).map(Cow::Borrowed)
    }

    fn decode_to_buf(&self, src: &[u8], dst: &mut BufMut<u8, 1>) -> Option<usize> {
        let src = as_ascii(src)?;
        dst.extend_from_slice(src.as_bytes());
        Some(src.len())
    }

}

pub struct Iso8859_1;
// SAFETY: Only ASCII is native, which is valid UTF-8, and characters are appended as UTF-8.
unsafe impl CodesetDef for Iso8859_1 {

    fn encode_to_buf(&self, src: &str, dst: &mut BufMut<u8, 1>) -> Option<usize> {
        encode_simple_buf(src, dst, encode_iso8859_1)
    }

    fn decode_is_native(&self, src: &[u8]) -> Option<bool> {
        // Every octet is valid, but only ASCII ones are the same in UTF-8.
        Some(src.is_ascii())
    }

    fn decode_to_cow<'a>(&self, src: &'a [u8]) -> Option<Cow<'a, str>> {
        Some(decode_simple_cow(src, 2, decode_iso8859_1))
    }

    fn decode_to_buf(&self, src: &[u8], dst: &mut BufMut<u8, 1>) -> Option<usize> {
        Some(decode_simple_buf(src, dst, 2, decode_iso8859_1))
    }

}

/// The octet of a character in ISO-8859-1, which is the value of its code point.
#[inline]
fn encode_iso8859_1(ch: char) -> Option<u8> {
    u8::try_from(ch).ok()
}

/// The character of an ISO-8859-1 octet, which is the code point of the same value.
#[inline]
fn decode_iso8859_1(b: u8) -> char {
    b as char
}

pub struct Windows1252;
// SAFETY: Only ASCII is native, which is valid UTF-8, and characters are appended as UTF-8.
unsafe impl CodesetDef for Windows1252 {

    fn encode_to_buf(&self, src: &str, dst: &mut BufMut<u8, 1>) -> Option<usize> {
        encode_simple_buf(src, dst, encode_windows1252)
    }

    fn decode_is_native(&self, src: &[u8]) -> Option<bool> {
        // Every octet is valid, but only ASCII ones are the same in UTF-8.
        Some(src.is_ascii())
    }

    fn decode_to_cow<'a>(&self, src: &'a [u8]) -> Option<Cow<'a, str>> {
        Some(decode_simple_cow(src, 3, decode_windows1252))
    }

    fn decode_to_buf(&self, src: &[u8], dst: &mut BufMut<u8, 1>) -> Option<usize> {
        Some(decode_simple_buf(src, dst, 3, decode_windows1252))
    }

}

/// The octet of a character in Windows-1252, like ISO-8859-1 but for the 0x80 to 0x9F
/// range, whose octets are other characters.
#[inline]
fn encode_windows1252(ch: char) -> Option<u8> {
    Some(match ch {
        '\u{0000}'..='\u{007F}' |
        '\u{00A0}'..='\u{00FF}' => ch as u8,
        '\u{2000}'..='\u{20FF}' => {
            let b = WINDOWS_1252_ENCODE_20XX[ch as u8 as usize];
            if b == 0 { return None } else { b }
        }
        '\u{0192}' => 0x83,
        '\u{02C6}' => 0x88,
        '\u{0160}' => 0x8A,
        '\u{0152}' => 0x8C,
        // Empty: 0x8D
        '\u{017D}' => 0x8E,
        // Empty: 0x8F
        // Empty: 0x90
        '\u{02DC}' => 0x98,
        '\u{2122}' => 0x99,
        '\u{0161}' => 0x9A,
        '\u{0153}' => 0x9C,
        // Empty: 0x9D
        '\u{017E}' => 0x9E,
        '\u{0178}' => 0x9F,
        _ => return None
    })
}

static WINDOWS_1252_ENCODE_20XX: [u8; 256] = const {
    let mut table = [0; 256];
    let mut i = 0;
    while i < WINDOWS_1252_DECODE_8X_9X.len() {
        let code = WINDOWS_1252_DECODE_8X_9X[i] as u32;
        if let 0x2000..=0x20FF = code {
            table[code as u8 as usize] = 0x80 + i as u8
        }
        i += 1;
    }
    table
};

/// The character of a Windows-1252 octet, like ISO-8859-1 but for the 0x80 to 0x9F range.
#[inline]
fn decode_windows1252(b: u8) -> char {
    match b {
        0x80..=0x9F => WINDOWS_1252_DECODE_8X_9X[b as usize - 0x80],
        _ => b as char,
    }
}

static WINDOWS_1252_DECODE_8X_9X: [char; 32] = [
    '€', '\0', '‚', 'ƒ', '„', '…', '†', '‡', 'ˆ', '‰', 'Š', '‹', 'Œ', '\0', 'Ž', '\0',
    '\0', '‘', '’', '“', '”', '•', '–', '—', '˜', '™', 'š', '›', 'œ', '\0', 'ž', 'Ÿ',
];

/// These octets as a string, when they are all ASCII.
#[inline]
fn as_ascii(src: &[u8]) -> Option<&str> {
    // SAFETY: ASCII is valid UTF-8.
    src.is_ascii().then(|| unsafe { std::str::from_utf8_unchecked(src) })
}

/// Characters encoded as one octet each, appended to the buffer, which first makes room for
/// as many octets as their UTF-8, which they are never more than, returning how many they
/// are. ASCII is copied as it is. Absent if a character has no octet, the buffer being then
/// left as it was.
fn encode_simple_buf(src: &str, dst: &mut BufMut<u8, 1>, encode: impl Fn(char) -> Option<u8>) -> Option<usize> {
    
    if src.is_ascii() {
        dst.extend_from_slice(src.as_bytes());
        return Some(src.len());
    }

    let start = dst.len();
    dst.reserve(src.len());
    for ch in src.chars() {
        match encode(ch) {
            Some(b) => dst.push(b),
            None => {
                dst.truncate(start);
                return None;
            }
        }
    }
    Some(dst.len() - start)

}

/// Octets decoded as one character each, of at most that many octets of UTF-8, borrowed
/// in place when they are all ASCII, and otherwise into a new string, see
/// [`decode_simple_cap`].
fn decode_simple_cow(src: &[u8], max_char_len: usize, decode: impl Fn(u8) -> char) -> Cow<'_, str> {
    if let Some(src) = as_ascii(src) {
        return Cow::Borrowed(src);
    }
    let mut s = String::with_capacity(decode_simple_cap(src.len(), max_char_len));
    s.extend(src.iter().map(|&b| decode(b)));
    Cow::Owned(s)
}

/// Octets decoded as one character each, of at most that many octets of UTF-8, appended
/// to the buffer, returning the length they take. ASCII is copied as it is, and otherwise
/// room is first made for them, see [`decode_simple_cap`].
fn decode_simple_buf(src: &[u8], dst: &mut BufMut<u8, 1>, max_char_len: usize, decode: impl Fn(u8) -> char) -> usize {
    
    if src.is_ascii() {
        dst.extend_from_slice(src);
        return src.len();
    }

    let start = dst.len();
    dst.reserve(decode_simple_cap(src.len(), max_char_len));
    for &b in src {
        dst.extend_from_slice(decode(b).encode_utf8(&mut [0; 4]).as_bytes());
    }
    dst.len() - start
    
}

/// The room to make for the UTF-8 of that many octets decoded as one character each, of
/// at most that many octets: their worst case when they are few, which wastes little, and
/// otherwise a guess of mostly ASCII text, what is decoded growing past it if it has to.
#[inline]
fn decode_simple_cap(len: usize, max_char_len: usize) -> usize {
    const WORST_CASE_UP_TO: usize = 64;
    if len <= WORST_CASE_UP_TO { len * max_char_len } else { len + len / 4 }
}

// ==================== //
//    Wide codesets     //
// ==================== //

/// Generic enumeration that can represent any supported codeset.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum WideCodeset {
    Utf16,
}

// SAFETY: Each wide codeset upholds it.
unsafe impl WideCodesetDef for WideCodeset {

    fn encode_to_buf(&self, src: &Utf16Str, dst: &mut BufMut<u16, 1>) -> Option<usize> {
        match self {
            WideCodeset::Utf16 => Utf16.encode_to_buf(src, dst),
        }
    }

    fn decode_is_native(&self, src: &[u16]) -> Option<bool> {
        match self {
            WideCodeset::Utf16 => Utf16.decode_is_native(src),
        }
    }

    fn decode_to_cow<'a>(&self, src: &'a [u16]) -> Option<Cow<'a, Utf16Str>> {
        match self {
            WideCodeset::Utf16 => Utf16.decode_to_cow(src),
        }
    }

    fn decode_to_buf(&self, src: &[u16], dst: &mut BufMut<u16, 1>) -> Option<usize> {
        match self {
            WideCodeset::Utf16 => Utf16.decode_to_buf(src, dst),
        }
    }

}

impl AnyCodeset for WideCodeset {

    const NATIVE: Self = Self::Utf16;
    const ALL: &[Self] = &[
        Self::Utf16,
    ];

    fn id(self) -> u32 {
        match self {
            Self::Utf16 => 0x00010109,
        }
    }

    fn from_id(id: u32) -> Option<Self> {
        Some(match id {
            0x00010109 => Self::Utf16,
            _ => return None
        })
    }

}

pub struct Utf16;
// SAFETY: Only valid UTF-16 is native, and appended.
unsafe impl WideCodesetDef for Utf16 {

    fn encode_to_buf(&self, src: &Utf16Str, dst: &mut BufMut<u16, 1>) -> Option<usize> {
        dst.extend_from_slice(src.as_slice());
        Some(src.len())
    }

    fn decode_is_native(&self, src: &[u16]) -> Option<bool> {
        Utf16Str::from_slice(src).is_ok().then_some(true)
    }

    fn decode_to_cow<'a>(&self, src: &'a [u16]) -> Option<Cow<'a, Utf16Str>> {
        Utf16Str::from_slice(src).ok().map(Cow::Borrowed)
    }

    fn decode_to_buf(&self, src: &[u16], dst: &mut BufMut<u16, 1>) -> Option<usize> {
        let src = Utf16Str::from_slice(src).ok()?;
        dst.extend_from_slice(src.as_slice());
        Some(src.len())
    }

}

// ==================== //
//     Negotiation      //
// ==================== //

/// From CORBA 3.4 Interroperability, Section 7.10.2.6 - Code Set Negotiation
/// 
/// Negociating with server codesets requirements, it returns an appropriate codeset.
pub fn negociate_with_server<C: AnyCodeset>(server_native_codeset: C, server_conversion_codesets: &[C]) -> Option<C> {
    
    if C::NATIVE == server_native_codeset {
        return Some(C::NATIVE);
    }

    if C::ALL.contains(&server_native_codeset) {
        return Some(server_native_codeset);
    }
    
    if server_conversion_codesets.contains(&C::NATIVE) {
        return Some(C::NATIVE);
    }

    for server_conversion_codeset in server_conversion_codesets {
        if C::ALL.contains(server_conversion_codeset) {
            return Some(*server_conversion_codeset);
        }
    }

    // TODO: definition of compatible codesets from both conversion codesets...

    None

}

/// A group of normal and wide codesets.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Codesets {
    pub codeset: Codeset,
    pub wide_codeset: WideCodeset,
}

#[cfg(test)]
mod tests {

    use super::*;

    /// Octets are only borrowed in place when they already are the UTF-8 of their string.
    #[test]
    fn in_place() {
        assert_eq!(Codeset::Utf8.decode_is_native("café".as_bytes()), Some(true));
        assert_eq!(Codeset::Utf8.decode_is_native(b"caf\xE9"), None);
        assert_eq!(Codeset::Ascii.decode_is_native(b"caf\xE9"), None);
        assert_eq!(Codeset::Iso8859_1.decode_is_native(b"caf\xE9"), Some(false));
        assert_eq!(Codeset::Windows1252.decode_is_native(b"caf\xE9"), Some(false));
        for codeset in [Codeset::Ascii, Codeset::Iso8859_1, Codeset::Windows1252] {
            assert_eq!(codeset.decode_is_native(b"cafe"), Some(true));
            assert_ne!(codeset.decode_is_native("café".as_bytes()), Some(true));
            assert!(matches!(codeset.decode_to_cow(b"cafe"), Some(Cow::Borrowed("cafe"))));
        }
    }

    /// Octets past ASCII are decoded into a new string.
    #[test]
    fn decoding_cow() {
        let decoded = Codeset::Iso8859_1.decode_to_cow(b"caf\xE9").unwrap();
        assert_eq!(decoded, "café");
        assert!(matches!(decoded, Cow::Owned(_)));
        assert_eq!(Codeset::Windows1252.decode_to_cow(b"\x80 \xE9").unwrap(), "€ é");
        assert_eq!(Codeset::Utf8.decode_to_cow(b"\xE9"), None);
        assert_eq!(Codeset::Ascii.decode_to_cow(b"caf\xE9"), None);
    }

    /// Decoding appends to what the buffer holds, which an invalid string leaves as it was.
    #[test]
    fn decoding_buf() {
        let mut buf = BufMut::from(&b"> "[..]);
        assert_eq!(Codeset::Iso8859_1.decode_to_buf(b"caf\xE9", &mut buf), Some(5));
        assert_eq!(Codeset::Windows1252.decode_to_buf(b" \x80 ", &mut buf), Some(5));
        assert_eq!(Codeset::Utf8.decode_to_buf("é".as_bytes(), &mut buf), Some(2));
        assert_eq!(Codeset::Ascii.decode_to_buf(b" ok", &mut buf), Some(3));
        assert_eq!(buf.as_slice(), "> café € é ok".as_bytes());
        assert_eq!(Codeset::Utf8.decode_to_buf(b"\xE9", &mut buf), None);
        assert_eq!(Codeset::Ascii.decode_to_buf(b"\xE9", &mut buf), None);
        assert_eq!(buf.as_slice(), "> café € é ok".as_bytes());
    }

    /// Encoding appends to what the buffer holds, which a character the codeset cannot
    /// encode leaves as it was, even past the ones it did.
    #[test]
    fn encoding() {
        let mut buf = BufMut::from(&b"> "[..]);
        assert_eq!(Codeset::Iso8859_1.encode_to_buf("café", &mut buf), Some(4));
        assert_eq!(Codeset::Windows1252.encode_to_buf(" € ", &mut buf), Some(3));
        assert_eq!(Codeset::Utf8.encode_to_buf("é", &mut buf), Some(2));
        assert_eq!(buf.as_slice(), b"> caf\xE9 \x80 \xC3\xA9");
        let len = buf.len();
        assert_eq!(Codeset::Iso8859_1.encode_to_buf("café €", &mut buf), None);
        assert_eq!(Codeset::Windows1252.encode_to_buf("café ✓", &mut buf), None);
        assert_eq!(Codeset::Ascii.encode_to_buf("café", &mut buf), None);
        assert_eq!(buf.len(), len);
    }

    /// Wide strings are borrowed in place when valid, and appended to a buffer.
    #[test]
    fn wide() {
        let units = widestring::utf16str!("café").as_slice();
        assert!(matches!(WideCodeset::Utf16.decode_to_cow(units), Some(Cow::Borrowed(s)) if s == "café"));
        assert_eq!(WideCodeset::Utf16.decode_is_native(units), Some(true));
        assert_eq!(WideCodeset::Utf16.decode_is_native(&[0xD800]), None);
        let mut buf = BufMut::new();
        assert_eq!(WideCodeset::Utf16.encode_to_buf(widestring::utf16str!("é€"), &mut buf), Some(2));
        assert_eq!(WideCodeset::Utf16.decode_to_buf(&[0xD800], &mut buf), None);
        assert_eq!(WideCodeset::Utf16.decode_to_buf(units, &mut buf), Some(4));
        assert_eq!(buf.as_slice(), widestring::utf16str!("é€café").as_slice());
    }

}