- View Types Introduction: A few years ago, the author proposed view types as an extension to Rust's type system to address false inter-procedural borrow conflicts. The basic idea is to introduce "view types" like
{f1, f2} Type
, meaning an instance ofType
where only specific fields can be accessed. - Example: The
Data
Type: TheData
type collects experiments with names andf32
values, along with a countersuccessful
. Helper functions are provided to access experiment names and data. - Tracking Successful Experiments Problem: Using the
Data
type as it is, there is a composition hazard. For example, when iterating over experiments and adjusting the successful counter, a borrow conflict occurs as theexperiment_names
function borrowsself
for the loop duration whileadd_successful
requires a mutable borrow. - Using View Types to Fix: By changing function signatures to
&{experiments} self
and&mut {successful} Self
, we can express what fields may be accessed. This allows the code to compile today and prevent future bugs. - Getting More Formal: View types can be integrated into Rust's type grammar, and a place expression is used to define a view onto a place. Checking field accesses against view types involves tracking the type of data and the allowed fields.
- Inferring Allow Sets: When type-checking calls to functions with view types, place-set variables need to be inferred. These variables are used in inference and integrated into HIR type check inference.
- Abstract Fields: Abstract fields allow us to address the problem of private field names becoming part of the interface. They are aliases for a set of fields and permit refactoring.
Frequently Asked Questions:
- Abstract fields can be mapped to an empty set.
- View types interact with traits and impls in various ways, such as implementing
Deref
for a view on a struct. - View types can include more complex paths like
{foo.bar} Baz
. - View types can be involved in moves.
- There are subtyping rules for view types.
- Abstract fields are useful in integrating with theorem provers and replacing phantom data.
- The parts of an abstract field include aliases for real fields, types for non-
()
abstract fields, and anonymous abstract fields. - View types can potentially be applied to other types in the future.
- Conclusion: The exploration of view types changed during writing. View types seem to be a useful type constructor on their own and need to be addressed in Rust. Prototype them in a-mir-formality to see if there are other surprises.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。