#My syntax highlight for egui

25 messages · Page 1 of 1 (latest)

summer ferry
#

I am not used to rust special types and ownership rules. I wrote this, and would like to know if it's any good. I have not implemented everything I would like to, but it works fine. I have three questions:

  1. Is this correct way to use Mutex or is there better way to do this?
  2. I feel like I am cloning too much. Can I avoid it?
  3. Am I using good coding practices? Code looks kinda messy.
    Here is my code: https://pastebin.com/SktTSH5g
indigo walrus
#
  1. are you using more than one thread or just using a static to pass data between functions
#

Mutex is normally used when you want a static that is mutable but thread safe

#

you can pass (&mut) the hashmap from the function that calls my_syntax_highlighter then pass (&mut) the hashmap into check_dump_keyword

#
  1. you can avoid cloning if you store borrows of String and TextFormat in the vecs, but they have to outlive the vec which is not always possible

and cloning is probably not the slowest part of the program

summer ferry
#

thanks for help. No I am not using more than one thread, but as I know lazy_static doesn't allow direct mut access to variable

indigo walrus
#
  1. the &mut Vec<_> can just be &mut [_]
#

run clippy for suggestions on code practices (cargo clippy)

indigo walrus
summer ferry
#

what other option do I have?

indigo walrus
summer ferry
#

so i store hashmap in editor (where it is called from) and give &mut access?

indigo walrus
#

yes

#

and can keyword_vec be a string

summer ferry
indigo walrus
#

&keyword as &str casts a &str to a &str i think

#
if KEYWORDS.contains(&(&keyword as &str)) {
    line_vec.push((keyword, color_theme.keyword.clone()));
} else {
    line_vec.push((keyword, color_theme.normal.clone()));
}

// can be

line_vec.push((keyword, if KEYWORDS.contains(&(&keyword as &str)) {
    color_theme.keyword
} else {
    color_theme.normal
}.clone()));

but it can be less readable

#
line_vec
  .iter()
  .for_each(|(slice, format)| job.append(&slice, 0.0, format.clone()));

i prefer a for loop

for (slice, format) in &line_vec { job.append(&slice, 0.0, format.clone()); }
summer ferry
indigo walrus
#

have you run clippy?

summer ferry
summer ferry
indigo walrus
#

or t if cond else f in python

summer ferry
#

thanks. I think that makes it much better