Skip to content

Instantly share code, notes, and snippets.

@Seeker14491
Created November 2, 2022 06:37
Show Gist options
  • Save Seeker14491/3e566ed1e5714ae013296996ce69732e to your computer and use it in GitHub Desktop.
Save Seeker14491/3e566ed1e5714ae013296996ce69732e to your computer and use it in GitHub Desktop.
use std::error::Error;
use ethers_providers::Provider;
trait ClientGeneric<T>
where
for<'a> &'a str: TryInto<Provider<T>>,
for<'a> <&'a str as TryInto<Provider<T>>>::Error: Error + 'static,
{
fn get_client(&self, url: &str) -> Result<Provider<T>, Box<dyn Error>> {
Ok(url.try_into()?)
}
}
impl<T, U> ClientGeneric<U> for T
where
for<'a> &'a str: TryInto<Provider<U>>,
for<'a> <&'a str as TryInto<Provider<U>>>::Error: Error + 'static,
{
}
struct Test;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_get_client_generic() {
let test = Test {};
let _client = test.get_client("http://localhost:8080").unwrap();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment