Rust part13at TECH
Rust part13 - 暇つぶし2ch124:デフォルトの名無しさん
21/11/24 17:26:56.67 Zq3lnaBh.net
>>106
なんとなく分かりましたがいきなりコンパイラが自動変換の前に現状で
例えばまずは整数型を例に絞ってやるとして
trait IntegerCompatible {
 type Integer;
 fn into_integer(&self) -> Self::Integer;
 fn from_integer(n: Self::Integer) -> Self;
}
こんな感じのトレイトにして
まずは利便性のために整数型自体に自分を返すよう実装しておいて
macro_rules! integer_compatible {
 ($($t:ty)*) => ($(
  impl IntegerCompatible for $t {
    type Integer = $t;
    #[inline]
    fn into_integer(&self) -> Self::Integer {
     *self
    }
    #[inline]
    fn from_integer(n: Self::Integer) -> Self {
     n
    }
  }
 )*)
}
integer_compatible! { i8 i16 i32 i64 i128 isize u8 u16 u32 u64 u128 usize }
あとは今はコンパイラ支援がないので
使う時に自分でinto_integerして計算などして結果をfrom_integerする感じですかね


次ページ
続きを表示
1を表示
最新レス表示
レスジャンプ
類似スレ一覧
スレッドの検索
話題のニュース
おまかせリスト
オプション
しおりを挟む
スレッドに書込
スレッドの一覧
暇つぶし2ch