Skip to content

Instantly share code, notes, and snippets.

@dherman
Created August 28, 2017 23:21
Show Gist options
  • Save dherman/84fbf925835d97089744f89e8160543f to your computer and use it in GitHub Desktop.
Save dherman/84fbf925835d97089744f89e8160543f to your computer and use it in GitHub Desktop.
example of an abstract class pattern failing to work with pub(crate)
pub(crate) trait EmployeePrivate {
fn unique_key(&self) -> i32;
}
pub struct Engineer {
key: i32,
name: String
}
pub struct EngineeringManager {
key: i32,
name: String,
reports: Vec<i32>
}
// etc...
// ERROR: private type `EmployeePrivate` leaked from public type `Employee`
pub trait Employee: EmployeePrivate {
fn name(&self) -> &str;
fn delete(self) {
let key = self.key(); // should work because every Employee is an EmployeePrivate
DB::delete_row(key);
}
}
impl EmployeePrivate for Engineer {
fn unique_key(&self) -> i32 { self.key }
}
impl Employee for Engineer {
fn name(&self) -> &str { &self.name }
}
impl EmployeePrivate for EngineeringManager {
fn unique_key(&self) -> i32 { self.key }
}
impl Employee for EngineeringManager {
fn name(&self) -> &str { &self.name }
}
pub trait Manager: Employee {
fn reports(&self) -> &[impl Employee];
}
impl Manager for EngineeringManager {
// etc...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment