Open KisaragiEffective opened 1 year ago
メモ: もし可変参照を安全に露出したい場合、RustのPinよりも強い制約を持ったスマポが必要である (具体的には置き換える操作と生の可変参照を露出する操作、及びチェックされない写像を安全ではないとマークし、チェックされる写像を安全とマークする。そして、有効期限が切れた際に走るデストラクタに置いて述語の検証をするようなスマポ。ただし、デストラクタで失敗するとプロセスが強制終了するしかなくなるのはどうなんだという感じ。)
これは言語機能のビルディングブロックにしたい。strは[u8]だし、StringはVec\<u8>。いずれも弱めることは容易いが、逆はそうではない。
動機
構文案
概要
R
というのは、ベースとなる型T
及びT
に属する値t
を制限する述語P
からなる。R
に対してT
の可変参照を要求する行為は安全である。可変参照を要求したとき、ラッパーが返される。ラッパーは変更が要求されるたびに可変参照が無効になったときにPによって検証され、もし条件を満たしていなければpanicする。または、手動で使うことをやめることもでき、その場合はそのやめたところでP
によって現在の値を検証する。P
による検証が走るが、panic
する代わりにResult
を返す。P
によって満たされる条件を満たさなければ未定義動作になる (R
によって保証される不変条件が壊れるため)。述語
And<P1, P2>
: P1とP2を同時に満たす述語。可換だがグラフの探索には時間がかかるかもしれないため可換ではないものとして扱う。Or<P1, P2>
: P1とP2のどちらか (あるいは両方) を同時に満たす述語。可換だがグラフの探索には時間がかかるかもしれないため可換ではないものとして扱う。Max<T: Ord, const-or-refined-value V: T>
: 最大値をV
に制限する。Min<T: Ord, const-or-refined-value V: T>
: 最小値をV
に制限する。Eq<T: Eq, const-or-refined-value V: T>
:V
であることを要求する。Not<T, P: Predicate<T>>:
P` が満たされないことを要求する。例
以下のRustの
enum
は、篩型で次のようにも表せる:NonZero
は次のように表せる:参照は次のように表せる (ここでは、ライフタイムの問題を無視していることに注意)