Want to try dedicating some amount of time at home to programming. My first guinea pig is a very trendy language Rust. So many people (and very smart people!) are crazy about it so even my very conservative and lazy brain agreed that “maybe there’s some gold in these hills”.

One more of my fascinating traits is that I’m a kind of stupid, so my first idea is to implement a triangle using Vulkan because why not. The big plan looks like this:

logging, math (just to familiarize myself with a rust a bit) -> import vulkan functions and simple wrappers around swapchains, command buffers, etc. -> ??? -> Profit!

Even implementing a very simple class diagram for logging happened to be quite a task.

General idea (pseudo-C++):

class Log
{
public:
    void write(severity, msg)
    {
        str = date + severity + msg;
        write_impl(str);
    }
private:
    virtual void write_impl(msg) = 0;
};

class ConsoleLog : public Log
{
    void write_impl(msg)
    {
        write_to_console(msg);
    }
};

Even putting aside my troubles with cargo I couldn’t implement that primitive sample in rust correctly:

pub trait Log
{
    fn write_impl(&self, str: &String) {}

    fn write(&self, severity: Severity, str: &String)
    {
        let log_str = format!("{} [{}] {}", time_str, severity_to_string(severity), str);
        self.write_impl(&log_str);
    }
}

pub struct ConsoleLog
{
}

impl Log for ConsoleLog
{
    fn write_impl(&self, str: &String)
    {
        println!("{}", str);
    }
}

Compiler happily spits out an error:

let mut log = log::base::ConsoleLog {};
log.write(log::base::Severity::debug, "abc");

error: no method named `write` found for type `log::base::ConsoleLog` in the current scope
 --> src/main.rs:6:9
  |
6 |     log.write(log::base::Severity::debug, "abc");
  |         ^^^^^
  |
  = help: items from traits can only be used if the trait is in scope; the following trait is implemented but not in scope, perhaps add a `use` for it:
  = help: candidate #1: `use log::base::Log`

I can’t even figure out what I’m doing wrong! Heh, time to read some forums.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s