Converting C-Like Enums to String values in Rust

Suppose you have a C-Like Enum in Rust like the one below:

enum Foo {
    SomeValue,
    OtherValue
}

Note: C-Like enumerations in Rust have no parameters (i.e. SomeValue(~str)), and can have their discriminator values explicitly set to a constant value (i.e. SomeValue = 0). See http://static.rust-lang.org/doc/master/tutorial.html#enums for details.

If you want to obtain the string value (i.e. “SomeValue”) given an unsigned integer, the trick is to first cast it as a Foo value, then use format to obtain its string representation. For the former step, first we need to make Foo implement the CLike trait:

extern mod extra;
 
use std::cast;
use extra::enum_set::CLike;
 
#[repr(uint)]
enum Foo {
    SomeValue,
    OtherValue
}
 
impl CLike for Foo {
    fn to_uint(&self) -> uint {
        *self as uint
    }
    fn from_uint(v: uint) -> Foo {
        unsafe { cast::transmute(v) }
    }
}

The latter step is easy, we just need to format the output as string. The simple way is to make Foo derive from ToStr:

extern mod extra;
 
use std::cast;
use extra::enum_set::CLike;
use std::to_str::ToStr;
 
#[repr(uint)]
#[deriving(ToStr)]
enum Foo {
    SomeValue,
    OtherValue
}
 
impl CLike for Foo {
    fn to_uint(&self) -> uint {
        *self as uint
    }
    fn from_uint(v: uint) -> Foo {
        unsafe { cast::transmute(v) }
    }
}
 
fn main() {
    let i : Foo = CLike::from_uint(1);
    println!("{}", i.to_str());
}

The output for this code is SomeValue.

Update: Thanks to Manishearth @ irc.mozilla.org for checking out some old links & typos in source code :-)


CSS Level 3 Text Decoration properties now available in Google Chrome Canary!

As of Monday (August 19th), Google Chrome Canary now supports CSS3 Text Decoration shorthand property. Once in Canary, you can enable the “experimental Web Platform features” runtime flag by checking the link below:

chrome://flags/#enable-experimental-web-platform-features

After enabling it, restart your browser and you should be able to use the following pattern:

text-decoration: underline dashed red;

As specified in CSS Level 3 Text Decoration shorthand specification. You can also check this page, with examples of shorthand property usage: http://www.abinader.com.br/css3-text-decoration-properties

I am also proposing these properties to go enabled by default in WebKit – stay tuned for further updates!


CSS Level 3 Text Decoration on WebKit and Blink – status

It’s been a while since I wrote the last post about progress on implementing CSS Level 3 Text Decoration features in WebKit. I’ve been involved with other projects but now I can finally resume the work in cooperation with my colleague from basysKom, Lamarque Souza. So far we have implemented:

  • text-decoration-line (link)
  • text-decoration-style (link)
  • text-decoration-color (link)
  • text-underline-position (link)

These properties are currently available under -webkit- prefix on WebKit, and guarded by a feature flag - CSS3_TEXT – which is enabled by default on both EFL and GTK ports. On Blink, plans are to get these properties unprefixed and under a runtime flag, which can be activated by enabling the “Experimental WebKit Features” (updated to “Experimental Web Platform Features” in latest builds) flag – see chrome://flags inside Google Chrome/Chromium). There are still some Skia-related issues to fix on Blink to enable proper dashed and dotted text decoration styles to be displayed. In the near future, we shall also have the text-decoration shorthand as specified on CSS Level 3 specification.

See below a summary of things I plan to finish in the near future:

  • [webkit] Property text-decoration-line now accepts blink as valid value
  • [blink] Fix implementation of dashed and dotted styles on Skia
  • [blink] Fix an issue where previous Skia stroke styles were used when rendering paint decorations
  • [blink] Implement CSS3_TEXT as a runtime flag
  • [blink] Property text-decoration-line now accepts blink as valid value
  • [blink] Implement support for text-decoration shorthand
  • [webkit] Implement support for text-decoration shorthand

Note: Please do not confuse text-decoration‘s blink value with Blink project :)

Stay tuned for further updates!


WebKit CSS3 text-decoration properties (preview)

WebKit currently supports CSS Text Level 2.1 version of text-decoration property (link). This version treats only about the decoration line types (underline, overline, line-through and blink – the latter is not supported on WebKit).

The draft version of CSS Text Level 3 upgrades text-decoration (link) property as a shorthand to 3 newly added properties, named text-decoration-line (link), text-decoration-style (link) and text-decoration-color (link), and also adds text-decoration-skip (link) property.

Among other WebKit stuff I’ve been doing lately, this feature implementation is one of the most cool ones I’m enjoying implementing. I’ve grabbed the task of implementing all of these CSS3 text-decoration* properties on WebKit, and results are great so far! Read the rest of this entry »


Vim Quicktip: Navigating to function implementation with ctags

I am a regular Vim user, but I’ve also played around with full blown IDEs like Qt Creator and MS Visual Studio. One interesting aspect about IDEs is the ability to easily navigate through a function implementation based on its usage on a piece of code. Taking the example from my CMake post, suppose you use the function sayHello() defined in a included header of your current source file and its implementation resides in another file as well. You may want to check out its implementation, but you doesn’t actually require to open a new view or loading it on the current view without an easy way to get back to the previous code. For this, Vim works very well with ctags. Let us check out how to generate ctags for your code and use it to let Vim know where to look up for your code.

Read the rest of this entry »


How CMake simplifies the build process (Part 1: Basic build system)

From my previous blogs there were some posts that got popular due to their useful content, and one that got my special attention is the tutorials on how to set up a basic (and further a bit more complex) build system based on CMake. These posts were left to dark when my contract with Dreamhost expired last year and I was lame enough to not save a backup. Lucky enough, I found them while browsing web.archives.org, which is actually a very good tool whenever you need to obtain information from previous versions of your website.

Wikipedia (link) defines CMake as:

CMake is a cross-platform, open-source system for managing the build process of software using a compiler-independent method. It is designed to support directory hierarchies and applications that depend on multiple libraries, and for use in conjunction with native build environments such as Make, Apple’s Xcode and Microsoft Visual Studio. It also has minimal dependencies, requiring only a C++-compiler on its own build system.

From my experience with build systems, CMake is one of the most flexible and easy to use ones, specially when you deal with a cross compilation environment (actually its name stands for Cross Platform Make). So let’s get started with the guide on how to set up a simple build system for a small project containing a few separate source files.

Read the rest of this entry »


A guide for Qt5/WebKit2 development setup for Nokia N9 on Ubuntu Linux

As part of my daily activities at basysKom on QtWebKit maintenance and development for Nokia devices, it is interesting to keep a track on latest developments circa QtWebKit. There is currently a promising project of a Qt5/WebKit2-based browser called Snowshoe mainly developed by my fellow friends from INdT which is completely open-source. This browser requires latest Qt5 and QtWebKit binaries and thus requires us to have a functional build system environment. There is a guide available on WebKit’s wiki (link) which is very helpful but lacks some information about compilation issues found when following the setup steps. So I am basing this guide from that wiki page and I hope that it gets updated soon :)

Read the rest of this entry »


2012: New year, new objectives!

Finland

Small river over Suomenlinna Island, near Helsinki

Hi all!

I’ve been out of blogging for a while, mostly because I got involved with a lot of non open-source activities during last year. So my plans for this year are to get back on track with my voluntary contributions as well as study new technologies, mostly like HTML5, CSS3, JavaScript and its hooks with WebKit (QtWebKit in particular).

Since last year, many things happened in my personal and professional life, including 2 different companies that I’ve worked to and now happily working at basysKom, a German-based company with passion for excellence and commitment to open-source. Things are great so far and I am now back to my hometown working remotely. You can expect to find some information about my experiences with Qt as well as other frameworks.

That’s it for now :)