質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
パラメータ

関数やプログラム実行時に与える設定値をパラメータと呼びます。

Rust

Rustは、MoFoが支援するプログラミング言語。高速性を維持しつつも、メモリ管理を安全に行うことが可能な言語です。同じコンパイル言語であるC言語やC++では困難だったマルチスレッドを実装しやすく、並行性という点においても優れています。

Q&A

解決済

1回答

1853閲覧

Rustでimplで宣言したconst Generic parameterがconstrainedされたないと怒られる

hidekiti

総合スコア23

パラメータ

関数やプログラム実行時に与える設定値をパラメータと呼びます。

Rust

Rustは、MoFoが支援するプログラミング言語。高速性を維持しつつも、メモリ管理を安全に行うことが可能な言語です。同じコンパイル言語であるC言語やC++では困難だったマルチスレッドを実装しやすく、並行性という点においても優れています。

0グッド

0クリップ

投稿2021/08/12 05:31

前提・実現したいこと

上のプログラムからエラーをなくしたい。

発生している問題・エラーメッセージ

the const parameter `N` is not constrained by the impl trait, self type, or predicates expressions using a const parameter must map each value to a distinct output value proving the result of expressions other than the parameter are unique is not supported rustc(E0207) const N: usize

該当のソースコード

rust

1impl<const N: usize> 2 Encrypted< 3 <TRLWE as Crypto<Polynomial<Binary, N>>>::PublicKey, 4 <TRLWE as Crypto<Polynomial<Binary, N>>>::Cipher, 5 > 6{ 7 pub fn sample_extract_index(&self, index: usize) -> ([Torus; N], Torus) { 8 let (a, b) = self; 9 let a_: [Torus; N] = array![ i => { 10 if i <= index { 11 a.coef_(index-i) 12 } 13 else { 14 a.coef_(N-i+index) 15 } 16 };N]; 17 let b_ = b.coef_(index); 18 (a_, b_) 19 } 20}

試したこと

impl先の型パラメータで使っているので、unconstrainedではないよなぁとなった。

補足情報(FW/ツールのバージョンなど)

rustc: rustc 1.56.0-nightly
注意:

rust

1#![feature(const_generics)] 2#![feature(const_evaluatable_checked)]

を使っている。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

"constrained" (「制約される」)という表現についてはドキュメントに記述されています。

Tが下の1., 2., 3.のいずれかを満たすとき、Tは実装を制約するといいます ("constrain an implementation")。

  1. Tがトレイトに表われる
    (e.g. impl<T> Trait<T> for Type {…})
  2. Tが型に表われる
    (e.g. impl<T> Trait for T {…}, impl<T> Type<T> {…})
  3. constraintしている他のパラメータの関連型の一つがTである
    (e.g. impl<O, B: ToOwned<Owned = O>> Trait<B> for Type {…})

たとえば以下のコードのimpl<D> <Impl as System<D>>::Inputにおいて、Dは型のパラメータではないので2.を満たさず、他のパラメータもないため3.も満たさないのでコンパイルに失敗します。

rust

1trait System<D> { 2 type Input; 3} 4 5struct SystemImpl; 6 7impl<D> System<D> for SystemImpl { 8 type Input = (); 9} 10 11impl<D> <SystemImpl as System<D>>::Input {}

text

1error[E0207]: the type parameter `D` is not constrained by the impl trait, self type, or predicates 2 --> src/lib.rs:11:6 3 | 411 | impl<D> <SystemImpl as System<D>>::Input {} 5 | ^ unconstrained type parameter

Generic parameters constrain an implementation if the parameter appears at least once in one of:

  • The implemented trait, if it has one
  • The implementing type
  • As an associated type in the bounds of a type that contains another parameter that constrains the implementation

6.12. Implementations - The Rust Reference

(少なくとも現在のRustでは)const parameterにも同様の制限が課せられています。

さて、解決法ですがNを浅いところに持っていくか、関数のパラメータにするという方針になると思います。後者であればTRLWE::sample_extract_index::<1024>(encrypted, index)のような形で呼ぶことになります。

rust

1impl TRLWE { 2 #[allow(clippy::type_complexity)] 3 pub fn sample_extract_index<const N: usize>( 4 encrypted: &Encrypted< 5 <Self as Crypto<Polynomial<Binary, N>>>::PublicKey, 6 <Self as Crypto<Polynomial<Binary, N>>>::Cipher, 7 >, 8 index: usize, 9 ) -> ([Torus; N], Torus) { 10 let (a, b) = encrypted; 11 todo!(); 12 } 13}

投稿2021/08/13 03:44

qryxip

総合スコア86

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問