diff options
author | iximeow <me@iximeow.net> | 2019-03-20 03:05:22 -0700 |
---|---|---|
committer | iximeow <me@iximeow.net> | 2020-01-12 16:26:39 -0800 |
commit | eb39d82930d4e68ca570a8d27df9498a4c1f65fb (patch) | |
tree | a7104aad99b3930abd2836c9c92444c7522ce163 /src/lib.rs | |
parent | d76a1cf75b0ae29147054d9f32a694e6a6d13ef0 (diff) |
add traits for syntax highlighting and contextualized display
these traits are not ideal but are what i can do in rust right now
the initial attempt to add these traits involved a trait providing a
`colorize` function that returned a struct (or tuple) that impl'd
Display but bundled all useful data together. this would be nice because
use would be like:
```
println!("the next instruction is: {}", instr.colorize(settings));
```
but this runs into the same kind of lifetime issues as mentioned in the
ContextRead commit.
additionally complicating is contextualization, which involves a larger
structure than just a copyable color setting - a similar
builder-and-display style use would be nice but involves a third
lifetime (the content, the colors, the contexts) and is not workable.
references could be Rc pointers and this all might work. why not do
that?
moral opposition to cloning Rc pointers when doing any instruction
rendering. might be worth revisiting later if i'm convinced this is too
annoying - it's already close.
meanwhile, it turns out implementing traits on tuples is currently not
possible anyway - tuples are not #[fundamental].
in the future i need to investigate if i can use macros to tie into
`derive`, so that a ShowContextual impl can #[derive(Colorize,
Display)]. those implementations would really just call
ShowContextual.colorize with fewer and fewer parameters non-None.
the blanket impl
```
impl <T, U> Colorize for T where T: ShowContextual<U>
```
is entirely just a relic of one attempt, and can be ignored. this idea
conflicted with coherence rules because someone else could impl Colorize
for some type covered under this blanket impl.
Diffstat (limited to 'src/lib.rs')
-rw-r--r-- | src/lib.rs | 43 |
1 files changed, 42 insertions, 1 deletions
@@ -2,7 +2,7 @@ extern crate num_traits; use std::str::FromStr; -use std::fmt::{Debug, Display}; +use std::fmt::{Debug, Display, Formatter}; use std::ops::{Add, Sub, AddAssign, SubAssign}; @@ -89,3 +89,44 @@ pub trait LengthedInstruction { fn len(&self) -> Self::Unit; fn min_size() -> Self::Unit; } + +pub struct ColorSettings { +} + +/* + * can this be a derivable trait or something? + */ +/* +impl <T: Colorize> Display for T { + fn fmt(&self, fmt: &mut Formatter) -> std::fmt::Result { + self.colorize(None, fmt) + } +} +*/ + +pub trait Colorize<T: std::fmt::Write> { + fn colorize(&self, colors: Option<&ColorSettings>, out: &mut T) -> std::fmt::Result; +} + +/* + * and make this auto-derive from a ShowContextual impl? + */ +/* +impl <T, U> Colorize for T where T: ShowContextual<Ctx=U> { + fn colorize(&self, colors: Option<&ColorSettings>, fmt: &mut Formatter) -> std::fmt::Result { + self.contextualize(colors, None, fmt) + } +} +*/ + +pub trait ShowContextual<Addr, Ctx: ?Sized, T: std::fmt::Write> { + fn contextualize(&self, colors: Option<&ColorSettings>, address: Addr, context: Option<&Ctx>, out: &mut T) -> std::fmt::Result; +} + +/* +impl <C: ?Sized, T: std::fmt::Write, U: Colorize<T>> ShowContextual<C, T> for U { + fn contextualize(&self, colors: Option<&ColorSettings>, context: Option<&C>, out: &mut T) -> std::fmt::Result { + self.colorize(colors, out) + } +} +*/ |