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(
align(left,
```javascript
// Computes the aspect ratio of the window.
let aspectRatio = window.innerWidth / window.innerHeight;
// 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);
```
raw(
read("../assets/dash-3d-implementation/base.js"),
block: true,
lang: "js",
),
),
caption: [A THREE.js _hello world_]
)<three-hello-world>
@ -92,23 +66,22 @@ Consider the piece of C++ code in @undefined-behaviour-cpp and @undefined-behavi
#figure(
align(left,
```cpp
auto vec = std::vector<int> {1, 2, 3};
for (auto value: vec)
vec.push_back(value);
)
```
raw(
read("../assets/dash-3d-implementation/undefined-behaviour.cpp"),
block: true,
lang: "cpp",
),
),
caption: [Undefined behaviour with for each syntax],
)<undefined-behaviour-cpp>
#figure(
align(left,
```cpp
auto vec = std::vector<int> {1, 2, 3};
for (auto it = std::begin(vec); it < std::end(vec); it++)
vec.push_back(*it);
```
raw(
read("../assets/dash-3d-implementation/undefined-behaviour-it.cpp"),
block: true,
lang: "cpp",
),
),
caption: [Undefined behaviour with iterator syntax],
)<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.
The equivalent code in Rust is in @undefined-behaviour-rs and @undefined-behaviour-it-rs.
#columns(2, gutter: 11pt)[
#v(0.8cm)
#grid(
columns: (1fr, 0.2fr, 1fr),
align(center + horizon)[
#figure(
align(left,
```rust
let mut vec = vec![1, 2, 3];
for value in &vec {
vec.push(value);
}
```
raw(
read("../assets/dash-3d-implementation/undefined-behaviour.rs"),
block: true,
lang: "rust",
),
),
caption: [Rust version of @undefined-behaviour-cpp],
)<undefined-behaviour-rs>
],
#colbreak()
[],
align(center + horizon)[
#figure(
align(left,
```rust
let mut vec = vec![1, 2, 3];
let iter = vec.iter();
loop {
match iter.next() {
Some(x) => vec.push(x),
None => break,
}
}
```
raw(
read("../assets/dash-3d-implementation/undefined-behaviour-it.rs"),
block: true,
lang: "rust",
),
),
caption: [Rust version of @undefined-behaviour-it-cpp],
)<undefined-behaviour-it-rs>
]
)
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(
align(left,
```
error[E0502]: cannot borrow `vec` as mutable because it is also borrowed as immutable
--> src/main.rs:4:9
|
3 | for value in &vec {
| ----
| |
| immutable borrow occurs here
| immutable borrow later used here
4 | vec.push(*value);
| ^^^^^^^^^^^^^^^^ mutable borrow occurs here
```
raw(
read("../assets/dash-3d-implementation/undefined-behaviour-error.txt"),
block: true,
),
),
caption: [Error given by the compiler on @undefined-behaviour-rs],
)<undefined-behaviour-error>