show pointer as Rust struct
authorRalf Jung <post@ralfj.de>
Thu, 26 Jul 2018 16:37:22 +0000 (18:37 +0200)
committerRalf Jung <post@ralfj.de>
Thu, 26 Jul 2018 16:37:22 +0000 (18:37 +0200)
ralf/_posts/2018-07-24-pointers-and-bytes.md

index 8f7089ec9086348d1ac7b623d88072e7a425d69c..35a5c159dbd9d32c4847f6e9af54ad0d63f2e066 100644 (file)
@@ -121,6 +121,13 @@ This is another example of using a "virtual machine" that's different from the r
 
 Here's a simple proposal (in fact, this is the model of pointers used in [CompCert](https://hal.inria.fr/hal-00703441/document) and my [RustBelt work]({% post_url 2017-07-08-rustbelt %}), and it is also how [miri](https://github.com/solson/miri/) implements [pointers](https://github.com/rust-lang/rust/blob/fefe81605d6111faa8dbb3635ab2c51d59de740a/src/librustc/mir/interpret/mod.rs#L121-L124)):
 A pointer is a pair of some kind of ID uniquely identifying the *allocation*, and an *offset* into the allocation.
+If we defined this in Rust, we might write
+{% highlight rust %}
+struct Pointer {
+    alloc_id: usize,
+    offset: isize,
+}
+{% endhighlight %}
 Adding/subtracting an integer to/from a pointer just acts on the offset, and can thus never leave the allocation.
 Subtracting a pointer from another is only allowed when both point to the same allocation (matching [C++](https://timsong-cpp.github.io/cppwp/n4140/expr.add#6)).[^2]