Read instead of raw, grid instead of cols

This commit is contained in:
Thomas Forgione 2023-04-18 10:52:41 +02:00
parent 6fa2bff614
commit 1f274d59c0
1 changed files with 47 additions and 84 deletions

View File

@ -28,37 +28,11 @@ A snippet of the basic usage of these classes is given in @three-hello-world.
#figure( #figure(
align(left, align(left,
```javascript raw(
// Computes the aspect ratio of the window. read("../assets/dash-3d-implementation/base.js"),
let aspectRatio = window.innerWidth / window.innerHeight; block: true,
lang: "js",
// Creates a camera and sets its parameters and position. ),
let camera = new THREE.PerspectiveCamera(70, aspectRatio, 0.01, 10);
camera.position.z = 1;
// Creates the scene that contains our objects.
let scene = new THREE.Scene();
// Creates a geometry (vertices and faces) corresponding to a cube.
let geometry = new THREE.BoxGeometry(0.2, 0.2, 0.2);
// Creates a material that paints the faces depending on their normal.
let material = new THREE.MeshNormalMaterial();
// Creates a mesh that associates the geometry with the material.
let mesh = new THREE.Mesh(geometry, material);
// Adds the mesh to the scene.
scene.add(mesh);
// Creates the renderer and append its canvas to the DOM.
renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
// Renders the scene with the camera.
renderer.render(scene, camera);
```
), ),
caption: [A THREE.js _hello world_] caption: [A THREE.js _hello world_]
)<three-hello-world> )<three-hello-world>
@ -92,23 +66,22 @@ Consider the piece of C++ code in @undefined-behaviour-cpp and @undefined-behavi
#figure( #figure(
align(left, align(left,
```cpp raw(
auto vec = std::vector<int> {1, 2, 3}; read("../assets/dash-3d-implementation/undefined-behaviour.cpp"),
for (auto value: vec) block: true,
vec.push_back(value); lang: "cpp",
) ),
```
), ),
caption: [Undefined behaviour with for each syntax], caption: [Undefined behaviour with for each syntax],
)<undefined-behaviour-cpp> )<undefined-behaviour-cpp>
#figure( #figure(
align(left, align(left,
```cpp raw(
auto vec = std::vector<int> {1, 2, 3}; read("../assets/dash-3d-implementation/undefined-behaviour-it.cpp"),
for (auto it = std::begin(vec); it < std::end(vec); it++) block: true,
vec.push_back(*it); lang: "cpp",
``` ),
), ),
caption: [Undefined behaviour with iterator syntax], caption: [Undefined behaviour with iterator syntax],
)<undefined-behaviour-it-cpp> )<undefined-behaviour-it-cpp>
@ -117,38 +90,36 @@ This loop should go endlessly because the vector grows in size as we add element
But the most important thing here is that since we add elements to the vector, it will eventually need to be reallocated, and that reallocation will invalidate the iterator, meaning that the following iteration will provoke an undefined behaviour. But the most important thing here is that since we add elements to the vector, it will eventually need to be reallocated, and that reallocation will invalidate the iterator, meaning that the following iteration will provoke an undefined behaviour.
The equivalent code in Rust is in @undefined-behaviour-rs and @undefined-behaviour-it-rs. The equivalent code in Rust is in @undefined-behaviour-rs and @undefined-behaviour-it-rs.
#columns(2, gutter: 11pt)[ #grid(
#v(0.8cm) columns: (1fr, 0.2fr, 1fr),
align(center + horizon)[
#figure( #figure(
align(left, align(left,
```rust raw(
let mut vec = vec![1, 2, 3]; read("../assets/dash-3d-implementation/undefined-behaviour.rs"),
for value in &vec { block: true,
vec.push(value); lang: "rust",
} ),
```
), ),
caption: [Rust version of @undefined-behaviour-cpp], caption: [Rust version of @undefined-behaviour-cpp],
)<undefined-behaviour-rs> )<undefined-behaviour-rs>
],
#colbreak() [],
align(center + horizon)[
#figure( #figure(
align(left, align(left,
```rust raw(
let mut vec = vec![1, 2, 3]; read("../assets/dash-3d-implementation/undefined-behaviour-it.rs"),
let iter = vec.iter(); block: true,
loop { lang: "rust",
match iter.next() { ),
Some(x) => vec.push(x),
None => break,
}
}
```
), ),
caption: [Rust version of @undefined-behaviour-it-cpp], caption: [Rust version of @undefined-behaviour-it-cpp],
)<undefined-behaviour-it-rs> )<undefined-behaviour-it-rs>
] ]
)
What happens is that the iterator needs to borrow the vector. What happens is that the iterator needs to borrow the vector.
@ -157,18 +128,10 @@ And effectively, the borrow checker will crash the compiler with the error in @u
#figure( #figure(
align(left, align(left,
``` raw(
error[E0502]: cannot borrow `vec` as mutable because it is also borrowed as immutable read("../assets/dash-3d-implementation/undefined-behaviour-error.txt"),
--> src/main.rs:4:9 block: true,
| ),
3 | for value in &vec {
| ----
| |
| immutable borrow occurs here
| immutable borrow later used here
4 | vec.push(*value);
| ^^^^^^^^^^^^^^^^ mutable borrow occurs here
```
), ),
caption: [Error given by the compiler on @undefined-behaviour-rs], caption: [Error given by the compiler on @undefined-behaviour-rs],
)<undefined-behaviour-error> )<undefined-behaviour-error>