mikecargal

mikecargal

Hands-on Rust: RGB:named(...) not necessary?

Title: Hands-on Rust:

Always found the RGB::named(…) thing to be a bit verbose. Then I noticed that one of your examples just used WHITE, and BLACK, in their place. Now, back in chapter 9, I’m back to seeing RGB::named(…).

Marked As Solved

herbert

herbert

Author of Hands-on Rust

My goodness, you’re right. That’s somewhat embarrassing on my end. It looks like I can get rid of the RGB::named and just use the constants. (I just changed a few at random and everything still works; I’ll get this updated for the next beta). Thank you for that - it makes the code look a LOT nicer.


Some history for how I improved that without realizing I’d fixed it.

Early in bracket-lib development, the color constants were all defined as tuple triplets. For example:

pub const BISQUE: (u8, u8, u8) = (255, 228, 196);

I thought that was a bit unwieldy, because RGB back then was pretty dumb and wouldn’t work without the named constructor. I implemented the From trait for RGB, allowing it to be constructed with RGB::from(NAMED_COLOR) or NAMED_COLOR.into(). That was a bit better, and more Rusty.

Anyway, a while later the terminal gained support for alpha transparency. Suddenly, I needed RGBA and not RGB everywhere! So all of the terminal functions became generic parameters accepting any type of TryInto<RGBA> - and conversion was added for RGB <-> RGBA. That was great, because you could use whichever one suited your problem domain and it would convert between them.

Using Into and TryInto gets a little complex, but it works remarkably well. The function signature for set is as follows:

pub fn set<COLOR, COLOR2, GLYPH, X, Y>(
        &mut self,
        x: X,
        y: Y,
        fg: COLOR,
        bg: COLOR2,
        glyph: GLYPH,
    ) where
        COLOR: Into<RGBA>,
        COLOR2: Into<RGBA>,
        GLYPH: TryInto<FontCharType>,
        X: TryInto<i32>,
        Y: TryInto<i32>,
    {

See how it uses generics (like you do for Vec<T>) with an additional where constraint? The color fields will accept anything that knows how to convert into an RGBA type. (When you implement From you get Into for free - one of the few times Rust isn’t explicit). So what’s with the TryInto? I wanted the user to be able to type any type of number they wanted, rather than having to remember that x is an i32 and so on. Not all numbers are readily convertible - and some numbers may be converted for some values and not others. For example, converting a signed integer into an unsigned integer doesn’t make sense for a negative number. TryInto attempts the conversion and throws an error our if the conversion fails at runtime.

It seriously never occurred to me that because RGB/RGBA have From<(u8, u8, u8)> defined it now automatically accepted the color constants.

So thank you! I learned something and the book code will be easier to read. :slight_smile:

(Edit: I should add that using traits and making your own is planned for the next beta. They are remarkably powerful)

Popular Pragmatic topics Top

Razor54672
The answer to 3rd Problem of Chapter 5 (Making Choices) of “Practical Programming, Third Edition” seems incorrect in the given answer ke...
New
yulkin
your book suggests to use Image.toByteData() to convert image to bytes, however I get the following error: "the getter ‘toByteData’ isn’t...
New
mikecargal
Title: Hands-On Rust (Chap 8 (Adding a Heads Up Display) It looks like ​.with_simple_console_no_bg​(SCREEN_WIDTH*2, SCREEN_HEIGHT*2...
New
lirux
Hi Jamis, I think there’s an issue with a test on chapter 6. I own the ebook, version P1.0 Feb. 2019. This test doesn’t pass for me: ...
New
herminiotorres
Hi! I know not the intentions behind this narrative when called, on page XI: mount() |&gt; handle_event() |&gt; render() but the correc...
New
rmurray10127
Title: Intuitive Python: docker run… denied error (page 2) Attempted to run the docker command in both CLI and Powershell PS C:\Users\r...
New
dsmith42
Hey there, I’m enjoying this book and have learned a few things alredayd. However, in Chapter 4 I believe we are meant to see the “&gt;...
New
hazardco
On page 78 the following code appears: &lt;%= link_to ‘Destroy’, product, class: ‘hover:underline’, method: :delete, data: { confirm...
New
AufHe
I’m a newbie to Rails 7 and have hit an issue with the bin/Dev script mentioned on pages 112-113. Iteration A1 - Seeing the list of prod...
New
Keton
When running the program in chapter 8, “Implementing Combat”, the printout Health before attack was never printed so I assumed something ...
New

Other popular topics Top

AstonJ
What chair do you have while working… and why? Is there a ‘best’ type of chair or working position for developers?
New
AstonJ
SpaceVim seems to be gaining in features and popularity and I just wondered how it compares with SpaceMacs in 2020 - anyone have any thou...
New
Rainer
My first contact with Erlang was about 2 years ago when I used RabbitMQ, which is written in Erlang, for my job. This made me curious and...
New
AstonJ
poll poll Be sure to check out @Dusty’s article posted here: An Introduction to Alternative Keyboard Layouts It’s one of the best write-...
New
Exadra37
I am a Linux user since 2012, more or less, and I always use Ubuntu on my computers, and my last 2 laptops have been used Thinkpads, wher...
New
PragmaticBookshelf
Author Spotlight James Stanier @jstanier James Stanier, author of Effective Remote Work , discusses how to rethink the office as we e...
New
AstonJ
If you get Can't find emacs in your PATH when trying to install Doom Emacs on your Mac you… just… need to install Emacs first! :lol: bre...
New
Help
I am trying to crate a game for the Nintendo switch, I wanted to use Java as I am comfortable with that programming language. Can you use...
New
PragmaticBookshelf
Author Spotlight: Tammy Coron @Paradox927 Gaming, and writing games in particular, is about passion, vision, experience, and immersio...
New
PragmaticBookshelf
Author Spotlight: Sophie DeBenedetto @SophieDeBenedetto The days of the traditional request-response web application are long gone, b...
New

Latest in PragProg

View all threads ❯