On 2013-11-15, at 2:40, Tommi <rusty.ga...@icloud.com> wrote:

> On 2013-11-15, at 2:09, Tommi <rusty.ga...@icloud.com> wrote:
> 
>> trait Inflate {
>>     fn get_radius<'s>(&'s mut self) -> &'s mut int;
>> 
>>     fn inflate_by(&mut self, amount: int) {
>>         *self.get_radius() += amount;
>>     }
>> }
>> 
>> trait Flatten {
>>     fn get_radius<'s>(&'s mut self) -> &'s mut int;
>> 
>>     fn release_all_air(&mut self) {
>>         *self.get_radius() = 0;
>>     }
>> }
>> 
>> struct Balloon {
>>     radius: int
>> }
>> 
>> impl Inflate for Balloon {
>>     fn get_radius<'s>(&'s mut self) -> &'s mut int {
>>         &mut self.radius
>>     }
>> }
>> 
>> impl Flatten for Balloon {
>>     fn get_radius<'s>(&'s mut self) -> &'s mut int {
>>         &mut self.radius
>>     }
>> }
> 
> 
> Am I correct in my assumption, that the above Rust code would translate 
> effectively the same kind of thing as the following C++ code?
> 
> template <typename T>
> struct Inflate {
>     void inflate_by(int amount)
>     {
>         static_cast<T*>(this)->ball_radius() += amount;
>     }
> };
> 
> template <typename T>
> struct Flatten {
>     void release_all_air()
>     {
>         static_cast<T*>(this)->balloon_size() = 0;
>     }
> };
> 
> struct Balloon : Inflate<Balloon>, Flatten<Balloon> {
>     int radius;
>     
>     int& ball_radius()  { return radius; }
>     int& balloon_size() { return radius; }
> };

Sorry, second attempt... would the above Rust code translate to something like 
this C++ code?

struct Inflate {
    virtual void inflate_by(int amount) = 0;
};

template <typename T>
struct InflateTemp : Inflate {
    void inflate_by(int amount) override
    {
        static_cast<T*>(this)->ball_radius() += amount;
    }
};

struct Flatten {
    virtual void release_all_air() = 0;
};

template <typename T>
struct FlattenTemp : Flatten {
    void release_all_air() override
    {
        static_cast<T*>(this)->balloon_size() = 0;
    }
};

struct Balloon : InflateTemp<Balloon>, FlattenTemp<Balloon> {
    int radius;
    
    int& ball_radius()  { return radius; }
    int& balloon_size() { return radius; }
};

-Tommi

_______________________________________________
Rust-dev mailing list
Rust-dev@mozilla.org
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to