From ddf944d808c71946a67d21d9fd1268ca05b787d1 Mon Sep 17 00:00:00 2001 From: Brian Cully Date: Sun, 24 Aug 2025 11:42:48 -0400 Subject: try some rusty stuff with std traits --- src/lib.rs | 60 +++++++++++++++++++++++++++++------------------------------- 1 file changed, 29 insertions(+), 31 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 71f7b7d..bb8e638 100755 --- a/src/lib.rs +++ b/src/lib.rs @@ -22,20 +22,22 @@ impl From<&forth::interp::InstructionPointer> for ExportedInstructionPointer { pub struct ExportedByteCode(Vec); impl ExportedByteCode { - pub fn from_bc(bc: &forth::interp::ByteCode) -> Self { - fn tr(op: &forth::interp::OpCode) -> String { - use forth::interp::OpCode::*; - let s = match op { - If(t, None) => format!("If({}, none)", t), - If(t, Some(f)) => format!("If({}, {})", t, f), - TIf(t, None) => format!("TIf({}, none)", t), - TIf(t, Some(f)) => format!("TIf({}, {})", t, f), - other => format!("{:?}", other), - }; - s.to_string() - } + fn tr_op(op: &forth::interp::OpCode) -> String { + use forth::interp::OpCode::*; + let s = match op { + If(t, None) => format!("If({}, none)", t), + If(t, Some(f)) => format!("If({}, {})", t, f), + TIf(t, None) => format!("TIf({}, none)", t), + TIf(t, Some(f)) => format!("TIf({}, {})", t, f), + other => format!("{:?}", other), + }; + s.to_string() + } +} - ExportedByteCode(bc.iter().map(tr).collect()) +impl From<&forth::interp::ByteCode> for ExportedByteCode { + fn from(v: &forth::interp::ByteCode) -> Self { + Self(v.iter().map(Self::tr_op).collect()) } } @@ -58,11 +60,11 @@ pub struct ExportedAnnotation { pub end: usize, } -impl From<(usize, usize)> for ExportedAnnotation { - fn from(v: (usize, usize)) -> Self { +impl From<&forth::parser::Annotation> for ExportedAnnotation { + fn from(v: &forth::parser::Annotation) -> Self { Self { - start: v.0, - end: v.1, + start: v.loc.0, + end: v.loc.1, } } } @@ -72,9 +74,9 @@ impl From<(usize, usize)> for ExportedAnnotation { #[derive(Clone)] pub struct ExportedWordAnnotations(Vec); -impl ExportedWordAnnotations { - pub fn from_word_annotations(annos: &Vec) -> Self { - ExportedWordAnnotations(annos.iter().map(|anno| anno.loc.into()).collect()) +impl FromIterator for ExportedWordAnnotations { + fn from_iter>(iter: T) -> Self { + ExportedWordAnnotations(iter.into_iter().collect()) } } @@ -107,16 +109,12 @@ impl ExportedInterp { error!("couldn't parse program text: {:?}", e); return false } - self.annos = p.annotations.iter() - .map(|word_anno| -> ExportedWordAnnotations { - let v = word_anno.into_iter() - .map(|anno| -> ExportedAnnotation { - anno.loc.into() - }) - .collect(); - ExportedWordAnnotations(v) - }) - .collect(); + self.annos = + p.annotations.iter() + .map(|word_anno| -> ExportedWordAnnotations { + word_anno.into_iter().map(|a| a.into()).collect() + }) + .collect(); let interp = forth::interp::Interp::new(p.wordlist); let _ = self.i.insert(interp); true @@ -152,7 +150,7 @@ impl ExportedInterp { return vec![] }; - interp.wordlist.iter().map(|bc| ExportedByteCode::from_bc(bc)).collect() + interp.wordlist.iter().map(|bc| bc.into()).collect() } pub fn annotations(&self) -> Vec { -- cgit v1.3