Add plumbing for displaying RocDec in the repl.

This commit is contained in:
Derek Gustafson 2022-03-04 17:32:09 -05:00
parent 874ce855bc
commit dc92de7781
No known key found for this signature in database
GPG key ID: 8B8B15EF3CC8410B
6 changed files with 61 additions and 18 deletions

View file

@ -4,6 +4,9 @@ use core::ffi::c_void;
use core::fmt;
use core::mem::{ManuallyDrop, MaybeUninit};
use core::ops::Drop;
use core::str;
// use roc_error_macros::internal_error;
// uncomment when we figure out why it fails below.
mod rc;
mod roc_list;
@ -277,4 +280,30 @@ impl RocDec {
pub fn from_str_to_i128_unsafe(val: &str) -> i128 {
Self::from_str(val).unwrap().0
}
fn to_str_helper(&self, bytes: &mut [u8; 1]) {
bytes[0] = 0;
// TODO
}
pub fn to_str(&self) -> RocStr {
let mut bytes: [u8; 1] = [0];
self.to_str_helper(&mut bytes);
unsafe {RocStr::from_slice(&bytes) }
}
}
impl fmt::Display for RocDec {
fn fmt(&self, fmtr: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut bytes: [u8; 1] = [0];
self.to_str_helper(&mut bytes);
match str::from_utf8(&bytes) {
Ok(slice) => write!(fmtr, "{}", slice),
Err(payload) => panic!("Error in converting RocDec({}) to a string: {}", self.0, payload),
// Err(payload) => internal_error!("Error in converting RocDec({}) to a string: {}", self.0, payload),
// This raises a compile error: can't find eprintln
// Is this because we don't use std?
}
}
}