Merge pull request #213 from urbit/eamsden/flog

add format-to-cord implementation and flog! macro
This commit is contained in:
Edward Amsden 2024-03-13 09:41:09 -05:00 committed by GitHub
commit 439657f35f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 74 additions and 0 deletions

73
rust/ares/src/flog.rs Normal file
View File

@ -0,0 +1,73 @@
use crate::interpreter::Context;
use crate::mem::NockStack;
use crate::noun::{Atom, IndirectAtom};
use std::fmt::Arguments;
use std::io::{Result, Write};
struct NockWriter<'s, 'b> {
stack: &'s mut NockStack,
buffer: &'b mut [u8],
indirect: IndirectAtom,
cursor: usize, //bytes
}
const INITIAL_CAPACITY_BYTES: usize = 256;
impl<'s, 'b> NockWriter<'s, 'b> {
unsafe fn new(stack: &'s mut NockStack) -> Self {
let (indirect, buffer) = IndirectAtom::new_raw_mut_bytes(stack, INITIAL_CAPACITY_BYTES);
NockWriter {
stack,
buffer,
indirect,
cursor: 0,
}
}
unsafe fn finalize(mut self) -> Atom {
self.indirect.normalize_as_atom()
}
unsafe fn expand(&mut self) {
let sz = self.buffer.len();
let (new_indirect, new_buffer) = IndirectAtom::new_raw_mut_bytes(self.stack, sz * 2);
new_buffer[0..sz].copy_from_slice(self.buffer);
self.buffer = new_buffer;
self.indirect = new_indirect;
}
}
impl Write for NockWriter<'_, '_> {
fn write(&mut self, buf: &[u8]) -> Result<usize> {
let sz = buf.len();
while (self.buffer.len() - self.cursor) < sz {
unsafe { self.expand() };
}
self.buffer[self.cursor..self.cursor + sz].copy_from_slice(buf);
self.cursor += sz;
Ok(sz)
}
fn flush(&mut self) -> Result<()> {
Ok(())
}
}
pub fn nock_fmt(context: &mut Context, fmt: Arguments<'_>) -> Result<Atom> {
let mut nw = unsafe { NockWriter::new(&mut context.stack) };
nw.write_fmt(fmt)?;
Ok(unsafe { nw.finalize() })
}
pub fn flog_fmt(context: &mut Context, fmt: Arguments<'_>) -> Result<()> {
let cord = nock_fmt(context, fmt)?;
context.newt.flog(&mut context.stack, cord.as_noun());
Ok(())
}
#[macro_export]
macro_rules! flog {
($ctx:expr, $($arg:tt)*) => {
$crate::flog::flog_fmt($ctx, std::format_args!($($arg)*))
}
}

View File

@ -3,6 +3,7 @@ extern crate num_derive;
extern crate lazy_static;
#[macro_use]
extern crate static_assertions;
pub mod flog;
pub mod guard;
pub mod hamt;
pub mod interpreter;