use encode_list_with with safe_encode_bits func

This commit is contained in:
Kasey White 2022-05-23 12:20:04 -04:00
parent c38da6426f
commit 344620136f
2 changed files with 12 additions and 3 deletions

View File

@ -137,10 +137,14 @@ impl Encoder {
}
}
pub fn encode_list_with<T: Encode>(&mut self, list: Vec<T>) -> Result<(), String> {
pub fn encode_list_with(
&mut self,
encoder_func: for<'r> fn(u8, &'r mut Encoder) -> Result<(), String>,
list: Vec<u8>,
) -> Result<(), String> {
for item in list {
self.one();
self.encode(item)?;
encoder_func(item, self)?;
}
self.zero();
Ok(())

View File

@ -3,6 +3,7 @@ use flat::en::{Encode, Encoder};
use crate::builtins::DefaultFunction;
const TERM_TAG_WIDTH: u32 = 4;
const CONST_TAG_WIDTH: u32 = 4;
#[derive(Debug)]
pub struct Program {
@ -70,7 +71,11 @@ pub enum Constant {
}
pub fn encode_constant(tag: u8, e: &mut Encoder) -> Result<(), String> {
e.encode_list_with([tag].to_vec())
e.encode_list_with(encode_constant_tag, [tag].to_vec())
}
pub fn encode_constant_tag(tag: u8, e: &mut Encoder) -> Result<(), String> {
safe_encode_bits(CONST_TAG_WIDTH, tag, e)
}
impl Encode for Program {