I am wondering what the best way to keep a record of operations used to create instances of a struct is, especially when creating many instances.
I have a type which I have overloaded some operations and functions, and I want to keep a record of what operation was performed to create each instance.
First method: simply using a str
for each operation.
struct Point {
x: i32,
y: i32,
op: &'static str,
}
impl Point {
fn new(x: i32, y: i32) -> Self {
Point {
x,
y,
op: "INITIALIZE",
}
}
}
impl std::ops::Add for Point {
type Output = Point;
fn add(self, other: Point) -> Point {
Point {
x: self.x + other.x,
y: self.y + other.y,
op: "ADDITION",
}
}
}
impl std::ops::Sub for Point {
type Output = Point;
fn sub(self, other: Point) -> Point {
Point {
x: self.x - other.x,
y: self.y - other.y,
op: "SUBTRACTION",
}
}
}
Second method: using an enum
for all the operations.
struct Point {
x: i32,
y: i32,
op: Operation,
}
enum Operation {
INITIALIZE,
ADDITION,
SUBTRACTION,
}
impl Point {
fn new(x: i32, y: i32) -> Self {
Point {
x,
y,
op: Operation::INITIALIZE,
}
}
}
impl std::ops::Add for Point {
type Output = Point;
fn add(self, other: Point) -> Point {
Point {
x: self.x + other.x,
y: self.y + other.y,
op: Operation::ADDITION,
}
}
}
impl std::ops::Sub for Point {
type Output = Point;
fn sub(self, other: Point) -> Point {
Point {
x: self.x - other.x,
y: self.y - other.y,
op: Operation::SUBTRACTION,
}
}
}
Or possibly another method ?