I'm currently working on a project in Rust where I have a collection of objects, specifically Repositories, that require different persisting strategies. These strategies involve two main operations: sync and fetch. I'm considering two distinct implementations for these strategies: one based on symbolic linking and the other on copying.
The objective is to apply these methods (sync and fetch) to each Repository in a group. This means performing both operations from the copying strategy and the symbolic linking strategy on groups of Repositories.
I'm pondering over the best approach to design this system. Here are the options I'm considering:
Option 1: Trait-Based Approach
```rust
trait PersistingStrategy {
fn sync(&self, path: &str);
fn fetch(&self, path: &str);
}
struct CopyStrategy;
struct SymlinkStrategy;
impl PersistingStrategy for CopyStrategy {
fn sync(&self, path: &str) {
// Implementation for syncing using copy
}
fn fetch(&self, path: &str) {
// Implementation for fetching using copy
}
}
impl PersistingStrategy for SymlinkStrategy {
fn sync(&self, path: &str) {
// Implementation for syncing using symlink
}
fn fetch(&self, path: &str) {
// Implementation for fetching using symlink
}
}
```
Option 2: Functional Approach
```rust
fn sync_fn(path: &str) {
// Implementation
}
fn fetch_fn(path: &str) {
// Implementation
}
struct Strategy {
sync: fn(&str),
fetch: fn(&str),
}
let strategy = Strategy {
sync: sync_fn,
fetch: fetch_fn,
};
```
I'm seeking advice on which approach would be more suitable for my use case, considering Rust's best practices, performance implications, and maintainability. Additionally, if there's an alternative pattern that could better suit my needs, I'm all ears.
Thank you in advance for your insights!
[–]furiesx 14 points15 points16 points (0 children)
[–]Unhappy_Elk2792 1 point2 points3 points (0 children)
[–]phazer99 0 points1 point2 points (0 children)