Skip to content

Instantly share code, notes, and snippets.

@afsalthaj
Created April 25, 2024 03:03
Show Gist options
  • Save afsalthaj/420736937ca0b505b44d5fa11f9f7aed to your computer and use it in GitHub Desktop.
Save afsalthaj/420736937ca0b505b44d5fa11f9f7aed to your computer and use it in GitHub Desktop.
AssociatedTypes In Rust
use std::collections::BTreeMap;
use std::fmt::Debug;
use std::cmp::PartialOrd;
use crate::internal_types::AI;
trait Conversion: Debug + Clone {
type WitType: Clone + Debug;
fn from_wit_type(input: Self::WitType) -> Result<Self, String>;
fn to_wit_type(&self) -> Result<Self::WitType, String>;
}
impl Conversion for AI {
type WitType = wit_types::A;
fn from_wit_type(input: Self::WitType) -> Result<Self, String> {
dbg!("Converting from wit type {}", input);
Ok(AI)
}
fn to_wit_type(&self) -> Result<Self::WitType, String> {
dbg!("Converting to wit type {}", self);
Ok(wit_types::A)
}
}
fn main() {
use Conversion;
let external_type: wit_types::A = wit_types::A;
let internal_type: internal_types::AI = internal_types::AI::from_wit_type(external_type).unwrap();
let external_type0 = internal_type.to_wit_type().unwrap();
dbg!(external_type0);
}
mod internal_types {
use super::*;
#[derive(Debug, Clone)]
pub(crate) struct AI;
struct BI;
struct CI;
}
mod wit_types {
#[derive(Clone, Debug)]
pub(crate) struct A;
pub(crate) struct B;
pub(crate) struct C;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment