Defer custom principal struct

To use a Principal as key of a StableBTreeMap I have to create a custom struct.

#[derive(CandidType, Deserialize, Clone, PartialOrd, Ord, Eq, PartialEq)]
pub struct StablePrincipal(pub(crate) Principal);

pub type StableControllerId = StablePrincipal;

impl From<&Principal> for StablePrincipal {
    fn from(principal: &Principal) -> Self {
        StablePrincipal(*principal)
    }
}

It works as expected, I can convert StablePrincipal::from(principal) and use the stable structure but, how can I defer the principal - how can I achieve the other way around?

Tried my_stable_principal.0 but got an issue

type `candid::Principal` cannot be dereferenced

Trie `my_stable_principal.into() but got an issue

 the trait `From<&StablePrincipal>` is not implemented for `candid::Principal`

Tried to implement a Defer trait but did not make it neither.

More a Rust noob question than related to the IC, still any help would be appreciated.

Not sure that’s the Rust way but a custom trait seems to do the job. If there is a more accurate way to solve this, please let me know!

impl StablePrincipal {
    pub fn to_principal(&self) -> Principal {
        self.0
    }
}

my_stable_principal.to_principal()

Using your code, my_stable_principal.0 works for me:

    let my_stable_principal: StablePrincipal = StablePrincipal::from(&Principal::from_slice(&[]));
    println!("{}", my_stable_principal.0);

No need for a custom trait or deref.

Looks like there is an extra * somewhere in your code. Post the whole line that is causing the error if you can’t find the extra *.

Ah cool! Thanks for double checking. Probably like you said an extra * somewhere or a borrow (I simplified the sample). I’ll have another look, good to know if works for you!