summaryrefslogtreecommitdiffstats
path: root/projects/learning-rust/2.html
diff options
context:
space:
mode:
Diffstat (limited to 'projects/learning-rust/2.html')
-rw-r--r--projects/learning-rust/2.html102
1 files changed, 102 insertions, 0 deletions
diff --git a/projects/learning-rust/2.html b/projects/learning-rust/2.html
new file mode 100644
index 0000000..c09b3f2
--- /dev/null
+++ b/projects/learning-rust/2.html
@@ -0,0 +1,102 @@
+<!DOCTYPE html>
+<html>
+<head>
+ <title>2: Solar Systems</title>
+
+ <link rel="stylesheet" href="/style.css"/>
+ <link rel="stylesheet" href="/prism/prism.css"/>
+ <script src="/prism/prism.js"></script>
+</head>
+<body>
+ <div class="window">
+ <div class="window-title"><h2 style="padding: 0px; margin: 0px;">Learning Rust : Solar Systems</h2></div>
+ <i>Everything is implicit and I don't know the exact types I'm working with.</i>
+ <div class="window-content">
+ <a class="link-button" href="/projects/learning-rust/1.html">&lt;</a>
+ <a class="link-button" href="/projects/learning-rust">Home</a>
+ <a class="link-button" href="/projects/learning-rust/3.html">&gt;</a>
+ <div/>
+ <p>So, now that I have a struct for defining orbitals, I can begin work on a container for these orbitals. I can store the orbitals in a vector, and iterate through the list of orbitals to tick and update them for a given time. We start with the following concept code:</p>
+ <pre><code class="language-rust">
+struct System
+{
+ orbitals : Vec&lt;Orbital&gt;
+}
+
+impl System
+{
+ pub fn tick(&self, t : Second)
+ {
+ let orbitals_len = self.orbitals.len();
+ for i in 0..orbitals_len {
+ let orbital = self.orbitals[i];
+ orbital.tick(self, t);
+ }
+ }
+}
+ </code></pre>
+ <p>This simple code leads me to my first major hurdle when dealing with Rust: Borrows against a container take the entire object, not just the element at a given index. Because of this quirk, I cannot borrow the parent orbital to calculate parts of my orbital state vectors. Since my "tick" function needs the parent orbital to calculate the gravitational parameter. Here's the code that fuels this problem:</p>
+ <pre><code class="language-rust">
+pub fn tick(&mut self, system: &System) -> ()
+{
+ ...
+ let origin_index = self.origin.unwrap();
+ let origin_orbital = system.orbitals[origin_index];
+ let origin_mass = origin_orbital.mass;
+ ...
+}
+ </code></pre>
+ <p>Because I can't borrow from the orbital vector more than once, I can't get the mass of the origin orbital.</p>
+ <p>Just to be clear, I understand why the borrow checker takes the entire vector when taking an element. If the element gets deleted or changed or the vector gets added to, then any other reference to the vector's elements must be re-evaluated. To properly borrow multiple mutable elements, I have to somehow split the vector in such a way that both the origin and ticking orbital are in seperate arrays.</p>
+ <p>Based on this information, and some help from the internet, here is what I came up with for accessing the parent-orbital pair</p>
+ <pre><code class="language-rust">
+fn orbital_set(&self, orbitals : &mut [Orbital], i : usize)
+ -&gt; (&mut Orbital, Option&lt;&mut Orbital&gt;)
+{
+ //The orbital vector must first be converted to a mutable span, so that I can access the raw data.
+ assert!(orbitals.len() &lt; i);
+ unsafe {
+ //From the orbital span, take a raw pointer from the given index, cast to a pointer to a mutable Orbital, and dereference.
+ let orbital = &mut *(orbitals.get_unchecked_mut(i) as *mut Orbital);
+ //Assuming the orbital is a valid object, check for the parent orbital.
+ match orbital.origin() {
+ Some(origin_i) =&gt; {
+ //Make sure the origin index does not break rules.
+ assert!(origin_i != i, "Orbital cannot orbit itself");
+ assert!(origin_i &lt; orbitals.len());
+ let origin = &mut *(orbitals.get_unchecked_mut(i) as *mut Orbital)
+ (orbital, Some(origin))
+ },
+ None =&gt; (orbital, None)
+ }
+ }
+}
+ </code></pre>
+ <p>I hate the unsafe block, but this is the only solution that I could find that had the characteristics that I wanted. Applied to the global tick method it looks like this:</p>
+ <pre><code class="language-rust">
+pub fn tick(&mut self) -&gt; ()
+{
+ let orbitals = &mut self.orbitals.borrow_mut()[..];
+ for i in 0..orbitals.len() {
+ let orbital_set = self.orbital_set(orbitals, i);
+ match orbital_set.1 {
+ Some(origin) =&gt; { orbital_set.0.tick(origin, t); }
+ None =&gt; {}
+ }
+ }
+}
+ </code></pre>
+ <p>where the orbital tick method looks like:</p>
+ <pre><code class="language-rust">
+pub fn tick(&mut self, origin : &mut Orbital, t : Second)
+{
+ ...
+ let origin_mass = origin.mass;
+ ...
+}
+ </code></pre>
+ <p>Now the challenge will be drawing all of this information on the screen!</p>
+ </div>
+ </div>
+</body>
+</html>