speice.io/_posts/2018-09-01-rusts-primitives-are-weird.md

317 lines
9.7 KiB
Markdown
Raw Normal View History

2018-08-22 23:17:04 -04:00
---
layout: post
2018-09-01 14:54:43 -04:00
title: "Rust's Primitives are Weird (and Cool)"
2018-08-22 23:17:04 -04:00
description: "but mostly weird."
category:
tags: [rust, c, java, python, x86]
---
2018-09-01 14:54:43 -04:00
I wrote a really small Rust program a while back because I was curious. I was 100% convinced it
couldn't possibly run:
2018-08-22 23:17:04 -04:00
```rust
fn main() {
println("{}", 8.to_string())
}
```
2018-09-01 14:54:43 -04:00
And to my complete befuddlement, it compiled, ran, and produced a completely sensible output.
2018-08-22 23:17:04 -04:00
The reason I was so surprised has to do with how Rust treats a special category of things
I'm going to call *primitives*. In the current version of the Rust book, you'll see them
2018-09-01 15:29:37 -04:00
referred to as [scalars](rust_scalar), and in older versions they'll be called [primitives](rust_primitive),
but we're going to stick with the name *primitive* for the time being. Explaining
2018-08-22 23:17:04 -04:00
why this program is so cool requires talking about a number of other programming languages,
and keeping a consistent terminology makes things easier.
**You've been warned:** this is going to be a tedious post about a relatively minor issue that involves
2018-09-01 14:54:43 -04:00
Java, Python, C, and x86 Assembly. And also me pretending like I know what I'm talking about with assembly.
2018-08-22 23:17:04 -04:00
# Defining primitives (Java)
2018-09-01 14:54:43 -04:00
The reason I'm using the name *primitive* comes from how much of my life is Java right now.
Spoiler alert: a lot of it. And for the most part I like Java, but I digress. In Java, there's a
special name for some specific types of values:
2018-08-22 23:17:04 -04:00
> ```
bool char byte
short int long
float double
```
They are referred to as [primitives][java_primitive]. And relative to the other bits of Java,
2018-09-01 15:29:37 -04:00
they have two unique features. First, they don't have to worry about the
2018-08-22 23:17:04 -04:00
[billion-dollar mistake](https://en.wikipedia.org/wiki/Tony_Hoare#Apologies_and_retractions);
primitives in Java can never be `null`. Second: *they can't have instance methods*.
Remember that Rust program from earlier? Java has no idea what to do with it:
```java
class Main {
public static void main(String[] args) {
int x = 8;
System.out.println(x.toString()); // Triggers a compiler error
}
}
```
The error is:
```
Main.java:5: error: int cannot be dereferenced
System.out.println(x.toString());
^
1 error
```
2018-09-01 15:29:37 -04:00
Specifically, Java's [`Object`](https://docs.oracle.com/javase/10/docs/api/java/lang/Object.html)
and things that inherit from it are pointers under the hood, and we have to dereference them before
the fields and methods they define can be used. In contrast, *primitive types are just values* -
2018-09-01 14:54:43 -04:00
there's nothing to be dereferenced. In memory, they're just a sequence of bits.
2018-08-22 23:17:04 -04:00
If we really want, we can turn the `int` into an
2018-09-01 14:54:43 -04:00
[`Integer`](https://docs.oracle.com/javase/10/docs/api/java/lang/Integer.html) and then
dereference it, but it's a bit wasteful:
2018-08-22 23:17:04 -04:00
```java
class Main {
public static void main(String[] args) {
int x = 8;
Integer y = Integer.valueOf(x);
System.out.println(y.toString());
}
}
```
2018-09-01 14:54:43 -04:00
This creates the variable `y` of type `Integer` (which inherits `Object`), and at run time we
dereference `y` to locate the `toString()` function and call it. Rust obviously handles things a bit
2018-09-01 15:29:37 -04:00
differently, but we have to dig into the low-level details to see it in action.
2018-08-22 23:17:04 -04:00
# Low Level Handling of Primitives (C)
2018-09-01 14:54:43 -04:00
We first need to build a foundation for reading and understanding the assembly code the
2018-09-01 15:29:37 -04:00
final answer requires. Let's begin with showing how the `C` language (and your computer)
2018-09-01 14:54:43 -04:00
thinks about "primitive" values in memory:
2018-08-22 23:17:04 -04:00
```c
void my_function(int num) {}
int main() {
int x = 8;
my_function(x);
}
```
2018-09-01 14:54:43 -04:00
The [compiler explorer](https://godbolt.org/z/lgNYcc) gives us an easy way of showing off
the assembly-level code that's generated: <span style="font-size:.6em">whose output has been lightly edited</span>
2018-08-22 23:17:04 -04:00
2018-09-01 14:54:43 -04:00
```nasm
2018-08-22 23:17:04 -04:00
main:
push rbp
mov rbp, rsp
sub rsp, 16
2018-09-01 14:54:43 -04:00
2018-08-22 23:17:04 -04:00
; We assign the value `8` to `x` here
mov DWORD PTR [rbp-4], 8
2018-09-01 14:54:43 -04:00
2018-08-22 23:17:04 -04:00
; And copy the bits making up `x` to a location
2018-09-01 14:54:43 -04:00
; `my_function` can access (`edi`)
2018-08-22 23:17:04 -04:00
mov eax, DWORD PTR [rbp-4]
mov edi, eax
2018-09-01 14:54:43 -04:00
; Call `my_function` and give it control
2018-08-22 23:17:04 -04:00
call my_function
2018-09-01 14:54:43 -04:00
2018-08-22 23:17:04 -04:00
mov eax, 0
leave
ret
my_function:
push rbp
mov rbp, rsp
2018-09-01 14:54:43 -04:00
; Copy the bits out of the pre-determined location (`edi`)
2018-08-22 23:17:04 -04:00
; to somewhere we can use
mov DWORD PTR [rbp-4], edi
nop
2018-09-01 14:54:43 -04:00
2018-08-22 23:17:04 -04:00
pop rbp
ret
```
2018-09-01 14:54:43 -04:00
At a really low level of memory, we're copying bits around using the [`mov`][x86_guide] instruction; nothing crazy.
But to show how similar Rust is, let's take a look at our program translated from C to Rust:
2018-08-22 23:17:04 -04:00
```rust
fn my_function(x: i32) {}
fn main() {
let x = 8;
my_function(x)
}
```
2018-09-01 14:54:43 -04:00
And the assembly generated when we stick it in the [compiler explorer](https://godbolt.org/z/cAlmk0):
<span style="font-size:.6em">again, lightly edited</span>
```nasm
2018-08-22 23:17:04 -04:00
example::main:
push rax
2018-09-01 14:54:43 -04:00
2018-08-22 23:17:04 -04:00
; Look familiar? We're copying bits to a location for `my_function`
; The compiler just optimizes out holding `x` in memory
mov edi, 8
2018-09-01 14:54:43 -04:00
; Call `my_function` and give it control
2018-08-22 23:17:04 -04:00
call example::my_function
2018-09-01 14:54:43 -04:00
2018-08-22 23:17:04 -04:00
pop rax
ret
example::my_function:
sub rsp, 4
2018-09-01 14:54:43 -04:00
2018-08-22 23:17:04 -04:00
; And copying those bits again, just like in C
mov dword ptr [rsp], edi
2018-09-01 14:54:43 -04:00
2018-08-22 23:17:04 -04:00
add rsp, 4
ret
```
2018-09-01 15:29:37 -04:00
The generated Rust assembly is functionally pretty close to the C assembly:
2018-09-01 14:54:43 -04:00
*When working with primitives, we're just dealing with bits in memory*.
2018-08-22 23:17:04 -04:00
2018-09-01 14:54:43 -04:00
In Java we have to dereference a pointer to call its functions; in Rust, there's no pointer to dereference. So what
exactly is going on with this `.to_string()` function call?
2018-08-22 23:17:04 -04:00
# impl primitive (and Python)
2018-09-01 14:54:43 -04:00
Now it's time to <strike>reveal my trap card</strike> show the revelation that tied all this together: *Rust has
2018-08-22 23:17:04 -04:00
implementations for its primitive types.* That's right, `impl` blocks aren't only for `structs` and `traits`,
primitives get them too. Don't believe me? Check out [u32](https://doc.rust-lang.org/std/primitive.u32.html),
[f64](https://doc.rust-lang.org/std/primitive.f64.html) and [char](https://doc.rust-lang.org/std/primitive.char.html)
as examples.
2018-09-01 14:54:43 -04:00
But the really interesting bit is how Rust turns those `impl` blocks into assembly. Let's break out the
2018-08-22 23:17:04 -04:00
[compiler explorer](https://godbolt.org/z/6LBEwq) once again:
```rust
pub fn main() {
8.to_string()
}
```
2018-09-01 14:54:43 -04:00
And the interesting bits in the assembly: <span style="font-size:.6em">heavily trimmed down</span>
2018-08-22 23:17:04 -04:00
2018-09-01 14:54:43 -04:00
```nasm
2018-08-22 23:17:04 -04:00
example::main:
sub rsp, 24
mov rdi, rsp
lea rax, [rip + .Lbyte_str.u]
mov rsi, rax
2018-09-01 14:54:43 -04:00
2018-09-01 15:29:37 -04:00
; Cool stuff right here
2018-08-22 23:17:04 -04:00
call <T as alloc::string::ToString>::to_string@PLT
2018-09-01 14:54:43 -04:00
2018-08-22 23:17:04 -04:00
mov rdi, rsp
call core::ptr::drop_in_place
add rsp, 24
ret
```
2018-09-01 14:54:43 -04:00
Now, this assembly is a bit more complicated, but here's the big revelation: **we're calling
`to_string()` as a function that exists all on its own, and giving it the instance of `8`**.
Instead of thinking of the value 8 as an instance of `u32` and then peeking in to find
the location of the function we want to call (like Java), we have a function that exists
outside of the instance and just give that function the value `8`.
2018-08-22 23:17:04 -04:00
This is an incredibly technical detail, but the interesting idea I had was this:
2018-09-01 14:54:43 -04:00
*if `to_string()` is a static function, can I refer to the unbound function and give it an instance?*
2018-08-22 23:17:04 -04:00
Better explained in code (and a [compiler explorer](https://godbolt.org/z/fJY-gA) link
because I seriously love this thing):
```rust
struct MyVal {
x: u32
}
impl MyVal {
fn to_string(&self) -> String {
self.x.to_string()
}
}
pub fn main() {
let my_val = MyVal { x: 8 };
2018-09-01 14:54:43 -04:00
// THESE ARE TOTALLY EQUIVALENT
2018-08-22 23:17:04 -04:00
my_val.to_string();
MyVal::to_string(&my_val);
}
```
Rust is totally fine "binding" the function call to the instance, and also as a static.
MIND == BLOWN.
2018-09-01 14:54:43 -04:00
Python does the same thing where I can both call functions bound to their instances
2018-08-22 23:17:04 -04:00
and also call as an unbound function where I give it the instance:
```python
class MyClass():
x = 24
def my_function(self):
print(self.x)
m = MyClass()
m.my_function()
MyClass.my_function(m)
```
2018-09-01 14:54:43 -04:00
And Python tries to make you *think* that primitives can have instance methods...
2018-08-22 23:17:04 -04:00
2018-09-01 14:54:43 -04:00
```python
2018-08-22 23:17:04 -04:00
>>> dir(8)
['__abs__', '__add__', '__and__', '__class__', '__cmp__', '__coerce__',
'__delattr__', '__div__', '__divmod__', '__doc__', '__float__', '__floordiv__',
...
'__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__',
...]
>>> # Theoretically `8.__str__()` should exist, but:
>>> 8.__str__()
File "<stdin>", line 1
8.__str__()
^
SyntaxError: invalid syntax
2018-09-01 14:54:43 -04:00
>>> # It will run if we assign it first though:
>>> x = 8
>>> x.__str__()
'8'
2018-08-22 23:17:04 -04:00
```
2018-09-01 14:54:43 -04:00
...but in practice it's a bit complicated.
2018-08-22 23:17:04 -04:00
So while Python handles binding instance methods in a way similar to Rust, it's still not able
to run the example we started with.
# Conclusion
This was a super-roundabout way of demonstrating it, but the way Rust handles incredibly minor details
2018-09-01 14:54:43 -04:00
like primitives leads to really cool effects. Primitives are optimized like C in how they have a
space-efficient memory layout, yet the language still has a lot of features I enjoy in Python
(like both instance and late binding).
2018-08-22 23:17:04 -04:00
2018-09-01 14:54:43 -04:00
And when you put it together, there are areas where Rust does cool things nobody else can;
as a quirky feature of Rust's type system, `8.to_string()` is actually valid code.
2018-08-22 23:17:04 -04:00
2018-09-01 14:54:43 -04:00
Now go forth and fool your friends into thinking you know assembly. This is all I've got.
2018-08-22 23:17:04 -04:00
[x86_guide]: http://www.cs.virginia.edu/~evans/cs216/guides/x86.html
[java_primitive]: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
[compiler_explorer]: https://godbolt.org/
[rust_scalar]: https://doc.rust-lang.org/book/second-edition/ch03-02-data-types.html#scalar-types
[rust_primitive]: https://doc.rust-lang.org/book/first-edition/primitive-types.html