My native language is not English.I will describe my problem as much as possible.Please forgive the strange grammar.
Rust has an ownership system. This is very different from traditional languages. Some times I'm confused about this.
In actual business service, some objects usually need to be holded on multiple places.But this seems to violate the rust ownership principle.
For example, I have write a game engine.There are two struct: Texture, Sprite
In other language, it may like this:
class Texture {
data: List<u8>,
}
class Sprite {
texture: Texture, // this is an object reference
rect: Rect,
}
Texture texture = Texture.load("some/path"); // a texture may be holded by more than one time
Sprite sprite1 = new Sprite(texture, new Rect(0, 0, 100, 100));
Sprite sprite2 = new Sprite(texture, new Rect(100, 100, 100, 100)); // it's OK!
But in rust, it must be:
struct Texture {
data: Vec[u8],
}
struct Sprite {
texture: Texture, // can not use like this, this is a moved object
rect: Rect,
}
let texture = Texture::load("some/path");
let sprite1 = Sprite::new(texture, Rect::new(0, 0, 100, 100));
let sprite2 = Sprite::new(texture, Rect::new(100, 100, 100, 100)); // This is an compiler error, because use moved texture.
There are also some solutions.
- Solutions 1:
struct Sprite {
texture: &Texture, // Error! Expected Lifetime parameter, --explain E0106
rect: Rect,
}
I know the reason for the error here. But I do not want to use <'a> to solve this.
Actually, object texture's really lives longer than sprites.
But this will introduce more problems about the life cycle.
- Solutions 2:
I more prefer this solution.
struct Sprite {
texture: Arc<Texture>, // Reference counter seems more reasonable
rect: Rect,
}
I think use reference counter is more reasonable semantically.
But this means I must export the Arc in API:
impl Sprite {
fn new(texture: Arc<Texture>, rect: Rect) -> Sprite {
Sprite { .. }
}
}
The result of this is there are a lot of Arc in APIs. And use Arc is forced.
I don't know if this is appropriate. And if may couse performance problems.
In addition, two solutions let texture can not be mut. This is also a problem.
I just use Texture and Sprite for the example above.
In actual business service, there are a lot of similar situations.
I don't know what the best practice in rust about this situation.
Can guys anyone give me some advice?
Thank you!