File: cxx_string.rs

package info (click to toggle)
rust-cxx 1.0.141-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,632 kB
  • sloc: cpp: 1,573; javascript: 124; sh: 11; makefile: 8
file content (55 lines) | stat: -rw-r--r-- 1,133 bytes parent folder | download
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
#![allow(
    clippy::items_after_statements,
    clippy::uninlined_format_args,
    clippy::unused_async
)]

use cxx::{let_cxx_string, CxxString};
use std::fmt::Write as _;

#[test]
fn test_async_cxx_string() {
    async fn f() {
        let_cxx_string!(s = "...");

        async fn g(_: &CxxString) {}
        g(&s).await;
    }

    // https://github.com/dtolnay/cxx/issues/693
    fn assert_send(_: impl Send) {}
    assert_send(f());
}

#[test]
fn test_display() {
    let_cxx_string!(s = b"w\"x\'y\xF1\x80\xF1\x80z");

    assert_eq!(format!("{}", s), "w\"x'y\u{fffd}\u{fffd}z");
}

#[test]
fn test_debug() {
    let_cxx_string!(s = b"w\"x\'y\xF1\x80z");

    assert_eq!(format!("{:?}", s), r#""w\"x'y\xf1\x80z""#);
}

#[test]
fn test_fmt_write() {
    let_cxx_string!(s = "");

    let name = "world";
    write!(s, "Hello, {name}!").unwrap();
    assert_eq!(s.to_str(), Ok("Hello, world!"));
}

#[test]
#[cfg(feature = "std")]
fn test_io_write() {
    let_cxx_string!(s = "");
    let mut reader: &[u8] = b"Hello, world!";

    std::io::copy(&mut reader, &mut s).unwrap();
    assert_eq!(s.to_str(), Ok("Hello, world!"));
}