Skip to content

Instantly share code, notes, and snippets.

@pscott
Last active October 27, 2019 14:54
Show Gist options
  • Save pscott/4d5b29c3a92d36e160516f9f70811cc8 to your computer and use it in GitHub Desktop.
Save pscott/4d5b29c3a92d36e160516f9f70811cc8 to your computer and use it in GitHub Desktop.
<3
trait MyTrait<U, T> {
fn signing_func(&self, challenger: &U, history: &[T]) -> Result<usize, &'static str>;
}
impl MyTrait<TypeA, TypeB>
for HistoryInfo<TypeB>
{
fn signing_func(
&self,
challenger: &TypeA,
history: &[TypeB],
) -> Result<usize, &'static str> {
should_sign_attestation(challenger, history).map_err(|_| "invalid attestation")
}
}
impl MyTrait<TypeC, TypeD>
for HistoryInfo<TypeD>
{
fn signing_func(
&self,
challenger: &TypeC,
history: &[TypeD],
) -> Result<usize, &'static str> {
should_sign_block(challenger, history)
}
}
#[derive(Debug)]
struct HistoryInfo<T: Encode + Decode + Clone> {
filename: String,
mutex: Arc<Mutex<Vec<T>>>,
}
impl<T: Encode + Decode + Clone> HistoryInfo<T> {
fn should_sign<U>(&self, challenger: &U) -> Result<usize, &'static str> {
let guard = self.mutex.lock();
let history = &guard[..];
self.signing_func(challenger, history)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment