On 2013-11-15, at 3:23, Tommi <[email protected]> wrote:
> On 2013-11-15, at 2:40, Tommi <[email protected]> wrote:
>
>> On 2013-11-15, at 2:09, Tommi <[email protected]> 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; }
> };
Third time's the charm. One more detail was still missing:
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;
void inflate_by(int amount) final override
{
return InflateTemp<Balloon>::inflate_by(amount);
}
void release_all_air() final override
{
return FlattenTemp<Balloon>::release_all_air();
}
int& ball_radius() { return radius; }
int& balloon_size() { return radius; }
};
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev