rune/runtime/
fmt.rs

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
use core::ptr::NonNull;

use crate::alloc::fmt::TryWrite;
use crate::alloc::{self, String};
use crate::runtime::VmResult;
use crate::Any;

/// A formatter for the rune virtual machine.
///
/// This is used as a receiver to functions implementing the [`DEBUG_FMT`]
/// and [`DISPLAY_FMT`] protocols.
///
/// [`DEBUG_FMT`]: crate::runtime::Protocol::DEBUG_FMT
/// [`DISPLAY_FMT`]: crate::runtime::Protocol::DISPLAY_FMT
#[derive(Any)]
#[rune(crate, item = ::std::fmt)]
pub struct Formatter {
    pub(crate) out: NonNull<dyn TryWrite>,
    pub(crate) buf: String,
}

impl Formatter {
    /// Format onto the given trywrite.
    pub(crate) fn format_with(
        out: &mut String,
        f: impl FnOnce(&mut Self) -> VmResult<()>,
    ) -> VmResult<()> {
        // SAFETY: Call to this function ensures that the formatter does not
        // outlive the passed in reference.
        let mut fmt = Formatter {
            out: NonNull::from(out),
            buf: String::new(),
        };
        f(&mut fmt)
    }

    #[inline]
    pub(crate) fn parts_mut(&mut self) -> (&mut dyn TryWrite, &str) {
        // SAFETY: Formatter constrution requires `out` to be valid.
        (unsafe { self.out.as_mut() }, &self.buf)
    }

    #[inline]
    pub(crate) fn buf_mut(&mut self) -> &mut String {
        &mut self.buf
    }
}

impl TryWrite for Formatter {
    #[inline]
    fn try_write_str(&mut self, s: &str) -> alloc::Result<()> {
        // SAFETY: Formatter constrution requires `out` to be valid.
        unsafe { self.out.as_mut().try_write_str(s) }
    }

    #[inline]
    fn try_write_char(&mut self, c: char) -> alloc::Result<()> {
        // SAFETY: Formatter constrution requires `out` to be valid.
        unsafe { self.out.as_mut().try_write_char(c) }
    }
}