{
"$type": "site.standard.document",
"bskyPostRef": {
"cid": "bafyreiaxqigokunhrfjjjhpm56prfeufmfjvn3papzjpkqoda3hker6mtm",
"uri": "at://did:plc:ivbknywyskln22er3nkssdhl/app.bsky.feed.post/3mogna5gunip2"
},
"path": "/t/selectable-named-impl-with-newtype-wrapper/24400#post_1",
"publishedAt": "2026-06-16T13:32:41.000Z",
"site": "https://internals.rust-lang.org",
"tags": [
"Draft: Named impl with Implementation Selection Variant",
"wild hacked named impls by withoutboats"
],
"textContent": "I tried my best to simulate my Draft: Named impl with Implementation Selection Variant in current edition. The orginal idea is from wild hacked named impls by withoutboats\n\n\n use std::marker::PhantomData;\n\n pub trait NamedImplBase {\n type Receiver: ?Sized;\n }\n\n // newtype wrapper\n //#[fundamental]\n #[repr(transparent)]\n pub struct Wrap<NamedImpl>\n where NamedImpl: NamedImplBase,\n NamedImpl::Receiver: Sized\n {\n pub value: NamedImpl::Receiver,\n pub phantom: PhantomData<NamedImpl>,\n }\n\n //#[fundamental]\n #[repr(transparent)]\n pub struct WrapRef<'a, NamedImpl>\n where NamedImpl: NamedImplBase\n {\n pub value: &'a NamedImpl::Receiver,\n pub phantom: PhantomData<NamedImpl>,\n }\n\n // convert methods and trait\n impl<NamedImpl> Wrap<NamedImpl>\n where NamedImpl: NamedImplBase,\n NamedImpl::Receiver: Sized\n {\n pub fn new(value: NamedImpl::Receiver) -> Wrap<NamedImpl> {\n Wrap {\n value,\n phantom: PhantomData,\n }\n }\n }\n\n impl<'a, NamedImpl> WrapRef<'a, NamedImpl>\n where NamedImpl: NamedImplBase\n {\n pub fn new(value: &'a NamedImpl::Receiver) -> WrapRef<'a, NamedImpl> {\n WrapRef {\n value,\n phantom: PhantomData,\n }\n }\n }\n\n // TODO WrapRefMut\n // TODO impl Deref DerefMut From Into\n\n // named impl base for ToString\n pub trait NamedToString : NamedImplBase {\n fn to_string(this: &Self::Receiver) -> String;\n }\n impl<NamedImpl> std::string::ToString for Wrap<NamedImpl>\n where NamedImpl: NamedToString,\n NamedImpl::Receiver: Sized\n {\n fn to_string(&self) -> String {\n NamedImpl::to_string(&self.value)\n }\n }\n\n impl<'a, NamedImpl> std::string::ToString for WrapRef<'a, NamedImpl>\n where NamedImpl: NamedToString\n {\n fn to_string(&self) -> String {\n NamedImpl::to_string(&self.value)\n }\n }\n\n // third party crate 1\n //impl ToString for [i32] use ToStringForIntSlice1 {}\n pub enum ToStringForIntSlice1 {}\n impl NamedImplBase for ToStringForIntSlice1 {\n type Receiver = [i32];\n }\n impl NamedToString for ToStringForIntSlice1 {\n fn to_string(this: &[i32]) -> String {\n String::from(\"ToStringForIntSlice1\")\n }\n }\n\n // third party crate 2\n //impl ToString for [i32] use ToStringForIntSlice2 {}\n pub enum ToStringForIntSlice2 {}\n impl NamedImplBase for ToStringForIntSlice2 {\n type Receiver = [i32];\n }\n impl NamedToString for ToStringForIntSlice2 {\n fn to_string(this: &[i32]) -> String {\n String::from(\"ToStringForIntSlice2\")\n }\n }\n\n // proxy / decorator\n pub struct ToStringProxy<T: ToString>(PhantomData<T>);\n impl<T: ToString> NamedImplBase for ToStringProxy<T> {\n type Receiver = T;\n }\n impl<T: ToString> NamedToString for ToStringProxy<T> {\n fn to_string(this: &T) -> String {\n let mut str = String::new();\n str.push_str(\"before \");\n str.push_str(&this.to_string());\n str.push_str(\" after\");\n return str;\n }\n }\n\n // bin\n fn main() {\n let arr = [1,2,3];\n\n //let a1 = &arr as &([i32] + ToStringForIntSlice1);\n let arr_with_alter_impl1 = WrapRef::<'_, ToStringForIntSlice1>::new(&arr);\n dbg!(arr_with_alter_impl1.to_string());\n\n let dyn_arr1: &dyn ToString = &arr_with_alter_impl1;\n dbg!(dyn_arr1.to_string());\n\n let arr_with_alter_impl2 = WrapRef::<'_, ToStringForIntSlice2>::new(&arr);\n dbg!(arr_with_alter_impl2.to_string());\n\n let dyn_arr2: &dyn ToString = &arr_with_alter_impl2;\n dbg!(dyn_arr2.to_string());\n\n let num = 42;\n dbg!(num.to_string());\n let num_proxy = Wrap::<ToStringProxy<i32>>::new(num);\n dbg!(num_proxy.to_string());\n }\n",
"title": "Selectable Named impl with Newtype Wrapper"
}