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; }
};
-Tommi
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev