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)

Where Next?

Popular Pragmatic Bookshelf topics Top

jimmykiang
This test is broken right out of the box… — FAIL: TestAgent (7.82s) agent_test.go:77: Error Trace: agent_test.go:77 agent_test.go:...
New
GilWright
Working through the steps (checking that the Info,plist matches exactly), run the demo game and what appears is grey but does not fill th...
New
mikecargal
Title: Hands-on Rust: question about get_component (page 295) (feel free to respond. “You dug you’re own hole… good luck”) I have somet...
New
jskubick
I’m running Android Studio “Arctic Fox” 2020.3.1 Patch 2, and I’m embarrassed to admit that I only made it to page 8 before running into ...
New
jskubick
I found an issue in Chapter 7 regarding android:backgroundTint vs app:backgroundTint. How to replicate: load chapter-7 from zipfile i...
New
hazardco
On page 78 the following code appears: &lt;%= link_to ‘Destroy’, product, class: ‘hover:underline’, method: :delete, data: { confirm...
New
taguniversalmachine
Hi, I am getting an error I cannot figure out on my test. I have what I think is the exact code from the book, other than I changed “us...
New
ggerico
I got this error when executing the plot files on macOS Ventura 13.0.1 with Python 3.10.8 and matplotlib 3.6.1: programming_ML/code/03_...
New
bjnord
Hello @herbert ! Trying to get the very first “Hello, Bracket Terminal!" example to run (p. 53). I develop on an Amazon EC2 instance runn...
New
davetron5000
Hello faithful readers! If you have tried to follow along in the book, you are asked to start up the dev environment via dx/build and ar...
New

Other popular topics Top

PragmaticBookshelf
Design and develop sophisticated 2D games that are as much fun to make as they are to play. From particle effects and pathfinding to soci...
New
AstonJ
You might be thinking we should just ask who’s not using VSCode :joy: however there are some new additions in the space that might give V...
New
New
AstonJ
Just done a fresh install of macOS Big Sur and on installing Erlang I am getting: asdf install erlang 23.1.2 Configure failed. checking ...
New
PragmaticBookshelf
Learn different ways of writing concurrent code in Elixir and increase your application's performance, without sacrificing scalability or...
New
New
AstonJ
Biggest jackpot ever apparently! :upside_down_face: I don’t (usually) gamble/play the lottery, but working on a program to predict the...
New
PragmaticBookshelf
Rails 7 completely redefines what it means to produce fantastic user experiences and provides a way to achieve all the benefits of single...
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
AstonJ
If you’re getting errors like this: psql: error: connection to server on socket “/tmp/.s.PGSQL.5432” failed: No such file or directory ...
New

Sub Categories: