Skip to content

Commit

Permalink
Rust: From and Into trait - Part 2
Browse files Browse the repository at this point in the history
Signed-off-by: Vineel Kovvuri <[email protected]>
  • Loading branch information
vineelkovvuri committed Jan 15, 2025
1 parent 9b725d1 commit deb4a32
Show file tree
Hide file tree
Showing 51 changed files with 424 additions and 247 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@ date: 2025-01-14 19:05:52
tags: ['Rust']
---

## Rust: Rust-Aligned-vs-Unaligned-Memory-Access
## Rust: Rust Aligned vs Unaligned Memory Access

Unlike C, Rust enforces some rules when trying to access memory. Mainly it
requires consideration to alignment of the data that we are trying to read.
Below code will illustrate the details.

```rust
#![allow(unused)]

fn main() {
let data: [u8; 10] = [0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA];

Expand Down
51 changes: 51 additions & 0 deletions content/blog/Rust-From-vs-Into-traits-Part-2/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
---
title: "Rust: From and Into trait - Part 2"
date: 2025-01-14 23:33:54
tags: ['Rust']
---

## Rust: From and Into trait usage

Knowing how Rust's `From` and `Into` traits work is one part of the story, but
being able to use them efficiently is another. In this code sample, we will
explore how to define functions or methods that accept parameters which can be
converted from and into the expected types.

In this code, we have two functions, `increment()` and `increment2()`, and we
will see how they work with the `From` trait implemented for the `Number` type.

```rust
struct Number(u64);

fn increment(n: Number) -> Number {
Number(n.0 + 1)
}

fn increment2<T>(n: T) -> Number
where
T: Into<Number>,
{
let number = n.into(); // Conversion is done by the method itself
Number(number.0 + 1)
}

impl From<u64> for Number {
fn from(value: u64) -> Self {
Self(value)
}
}

fn main() {
// Just because we have implemented the From<u64> do not make the conversion
// from 10 to Number implicit. Below line do not compile.
let num = increment(10);
// You have to explicitly request the conversion by calling .into()
let num = increment(10.into());
// Or even better, make the increment2 method accept any type that can be
// converted into a Number. Since u64 can be converted into Number (as it
// implements From<u64>), increment2() can call the .into() method itself to
// perform the conversion. At the call site, we simply pass the u64 as a
// parameter.
let num = increment2(10);
}
```
2 changes: 1 addition & 1 deletion docs/404.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<!DOCTYPE html>
<html lang="en-us">
<head><script src="/livereload.js?mindelay=10&amp;v=2&amp;port=9999&amp;path=livereload" data-no-instant defer></script>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>404 Page not found | Vineel Kovvuri</title>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<!DOCTYPE html>
<html lang="en-us">
<head><script src="/livereload.js?mindelay=10&amp;v=2&amp;port=9999&amp;path=livereload" data-no-instant defer></script>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>A Newbie&#39;s Introduction To Compilation Process And Reverse Engineering | Vineel Kovvuri</title>
Expand Down
2 changes: 1 addition & 1 deletion docs/blog/c-case-studies/f/index.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<!DOCTYPE html>
<html lang="en-us">
<head><script src="/livereload.js?mindelay=10&amp;v=2&amp;port=9999&amp;path=livereload" data-no-instant defer></script>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Libspng - C Language Case Study | Vineel Kovvuri</title>
Expand Down
2 changes: 1 addition & 1 deletion docs/blog/compiler-internals/index.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<!DOCTYPE html>
<html lang="en-us">
<head><script src="/livereload.js?mindelay=10&amp;v=2&amp;port=9999&amp;path=livereload" data-no-instant defer></script>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Compiler Internals | Vineel Kovvuri</title>
Expand Down
2 changes: 1 addition & 1 deletion docs/blog/how-do-breakpoints-work-in-debuggers/index.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<!DOCTYPE html>
<html lang="en-us">
<head><script src="/livereload.js?mindelay=10&amp;v=2&amp;port=9999&amp;path=livereload" data-no-instant defer></script>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>How Do Breakpoints Work In Debuggers? | Vineel Kovvuri</title>
Expand Down
7 changes: 6 additions & 1 deletion docs/blog/index.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<!DOCTYPE html>
<html lang="en-us">
<head><script src="/livereload.js?mindelay=10&amp;v=2&amp;port=9999&amp;path=livereload" data-no-instant defer></script>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Blogs | Vineel Kovvuri</title>
Expand Down Expand Up @@ -35,6 +35,11 @@ <h1>Blog</h1>



<li>
<span class="date">2025/01/14</span>
<a href="/blog/rust-from-vs-into-traits-part-2/">Rust: From and Into trait - Part 2 - blog</a>
</li>

<li>
<span class="date">2025/01/14</span>
<a href="/blog/rust-aligned-vs-unaligned-memory-access/">Rust: Aligned vs Unaligned Memory Access - blog</a>
Expand Down
71 changes: 39 additions & 32 deletions docs/blog/index.xml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/blog/lib-files-101/index.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<!DOCTYPE html>
<html lang="en-us">
<head><script src="/livereload.js?mindelay=10&amp;v=2&amp;port=9999&amp;path=livereload" data-no-instant defer></script>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Lib Files 101 | Vineel Kovvuri</title>
Expand Down
2 changes: 1 addition & 1 deletion docs/blog/pci-express-basics-101/index.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<!DOCTYPE html>
<html lang="en-us">
<head><script src="/livereload.js?mindelay=10&amp;v=2&amp;port=9999&amp;path=livereload" data-no-instant defer></script>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>PCI Express Basics 101 | Vineel Kovvuri</title>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<!DOCTYPE html>
<html lang="en-us">
<head><script src="/livereload.js?mindelay=10&amp;v=2&amp;port=9999&amp;path=livereload" data-no-instant defer></script>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Pdb Files: The Glue Between The Binary File And Source Code | Vineel Kovvuri</title>
Expand Down Expand Up @@ -287,7 +287,7 @@ <h1 id="windbg-symbols-and-sources-search-heuristics">Windbg Symbols and Sources
</span></span><span style="display:flex;"><span>00007ff9<span style="color:#d20;background-color:#fff0f0">`</span>2f400000 00007ff9<span style="color:#d20;background-color:#fff0f0">`</span>2f5e0000 ntdll (<span style="color:#038">export</span> symbols)
</span></span><span style="display:flex;"><span>C:<span style="color:#04d;background-color:#fff0f0">\W</span>INDOWS<span style="color:#04d;background-color:#fff0f0">\S</span>YSTEM32<span style="color:#04d;background-color:#fff0f0">\n</span>tdll.dll
</span></span></code></pre></div><p>Looking at the .reload command output we can say Windbg expects symbol files to
be present inside the same directory as the application or the folder named &rsquo;exe&rsquo;
be present inside the same directory as the application or the folder named &rsquo;exe'
inside .sympath directory or the folder named &lsquo;dll&rsquo; inside the .sympath directory
or at the actual embedded path. Running lm confirms that symbols are recognized
for Win32Sample.exe</p>
Expand Down
8 changes: 3 additions & 5 deletions docs/blog/rust-aligned-vs-unaligned-memory-access/index.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<!DOCTYPE html>
<html lang="en-us">
<head><script src="/livereload.js?mindelay=10&amp;v=2&amp;port=9999&amp;path=livereload" data-no-instant defer></script>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Rust: Aligned vs Unaligned Memory Access | Vineel Kovvuri</title>
Expand Down Expand Up @@ -33,13 +33,11 @@ <h2 class="date">2025/01/14</h2>
</div>

<main>
<h2 id="rust-rust-aligned-vs-unaligned-memory-access">Rust: Rust-Aligned-vs-Unaligned-Memory-Access</h2>
<h2 id="rust-rust-aligned-vs-unaligned-memory-access">Rust: Rust Aligned vs Unaligned Memory Access</h2>
<p>Unlike C, Rust enforces some rules when trying to access memory. Mainly it
requires consideration to alignment of the data that we are trying to read.
Below code will illustrate the details.</p>
<div class="highlight"><pre tabindex="0" style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-rust" data-lang="rust"><span style="display:flex;"><span><span style="color:#c00;font-weight:bold">#![allow(unused)]</span><span style="color:#bbb">
</span></span></span><span style="display:flex;"><span><span style="color:#bbb">
</span></span></span><span style="display:flex;"><span><span style="color:#bbb"></span><span style="color:#080;font-weight:bold">fn</span> <span style="color:#06b;font-weight:bold">main</span>()<span style="color:#bbb"> </span>{<span style="color:#bbb">
<div class="highlight"><pre tabindex="0" style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-rust" data-lang="rust"><span style="display:flex;"><span><span style="color:#080;font-weight:bold">fn</span> <span style="color:#06b;font-weight:bold">main</span>()<span style="color:#bbb"> </span>{<span style="color:#bbb">
</span></span></span><span style="display:flex;"><span><span style="color:#bbb"> </span><span style="color:#080;font-weight:bold">let</span><span style="color:#bbb"> </span>data: [<span style="color:#888;font-weight:bold">u8</span>;<span style="color:#bbb"> </span><span style="color:#00d;font-weight:bold">10</span>]<span style="color:#bbb"> </span>=<span style="color:#bbb"> </span>[<span style="color:#00d;font-weight:bold">0x11</span>,<span style="color:#bbb"> </span><span style="color:#00d;font-weight:bold">0x22</span>,<span style="color:#bbb"> </span><span style="color:#00d;font-weight:bold">0x33</span>,<span style="color:#bbb"> </span><span style="color:#00d;font-weight:bold">0x44</span>,<span style="color:#bbb"> </span><span style="color:#00d;font-weight:bold">0x55</span>,<span style="color:#bbb"> </span><span style="color:#00d;font-weight:bold">0x66</span>,<span style="color:#bbb"> </span><span style="color:#00d;font-weight:bold">0x77</span>,<span style="color:#bbb"> </span><span style="color:#00d;font-weight:bold">0x88</span>,<span style="color:#bbb"> </span><span style="color:#00d;font-weight:bold">0x99</span>,<span style="color:#bbb"> </span><span style="color:#00d;font-weight:bold">0xAA</span>];<span style="color:#bbb">
</span></span></span><span style="display:flex;"><span><span style="color:#bbb">
</span></span></span><span style="display:flex;"><span><span style="color:#bbb"> </span><span style="color:#888">// Alignment Tidbit: Alignment depends on the data type we are using. u8 has
Expand Down
96 changes: 96 additions & 0 deletions docs/blog/rust-from-vs-into-traits-part-2/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<!DOCTYPE html>
<html lang="en-us">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Rust: From and Into trait - Part 2 | Vineel Kovvuri</title>
<link rel="stylesheet" href="/css/style.css" />
<link rel="stylesheet" href="/css/fonts.css" />
<link href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/github.min.css" rel="stylesheet">

</head>

<body>
<nav>
<ul class="menu">

<li><a href="/">Home</a></li>

<li><a href="/blog/">Blog</a></li>

<li><a href="/presentations/">Presentations</a></li>

<li><a href="/index.xml">Subscribe</a></li>

</ul>
<hr/>
</nav>

<div class="article-meta">
<h1><span class="title">Rust: From and Into trait - Part 2</span></h1>

<h2 class="date">2025/01/14</h2>
</div>

<main>
<h2 id="rust-from-and-into-trait-usage">Rust: From and Into trait usage</h2>
<p>Knowing how Rust&rsquo;s <code>From</code> and <code>Into</code> traits work is one part of the story, but
being able to use them efficiently is another. In this code sample, we will
explore how to define functions or methods that accept parameters which can be
converted from and into the expected types.</p>
<p>In this code, we have two functions, <code>increment()</code> and <code>increment2()</code>, and we
will see how they work with the <code>From</code> trait implemented for the <code>Number</code> type.</p>
<div class="highlight"><pre tabindex="0" style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-rust" data-lang="rust"><span style="display:flex;"><span><span style="color:#080;font-weight:bold">struct</span> <span style="color:#b06;font-weight:bold">Number</span>(<span style="color:#888;font-weight:bold">u64</span>);<span style="color:#bbb">
</span></span></span><span style="display:flex;"><span><span style="color:#bbb">
</span></span></span><span style="display:flex;"><span><span style="color:#bbb"></span><span style="color:#080;font-weight:bold">fn</span> <span style="color:#06b;font-weight:bold">increment</span>(n: <span style="color:#b06;font-weight:bold">Number</span>)<span style="color:#bbb"> </span>-&gt; <span style="color:#b06;font-weight:bold">Number</span><span style="color:#bbb"> </span>{<span style="color:#bbb">
</span></span></span><span style="display:flex;"><span><span style="color:#bbb"> </span>Number(n.<span style="color:#00d;font-weight:bold">0</span><span style="color:#bbb"> </span>+<span style="color:#bbb"> </span><span style="color:#00d;font-weight:bold">1</span>)<span style="color:#bbb">
</span></span></span><span style="display:flex;"><span><span style="color:#bbb"></span>}<span style="color:#bbb">
</span></span></span><span style="display:flex;"><span><span style="color:#bbb">
</span></span></span><span style="display:flex;"><span><span style="color:#bbb"></span><span style="color:#080;font-weight:bold">fn</span> <span style="color:#06b;font-weight:bold">increment2</span>&lt;T&gt;(n: <span style="color:#b06;font-weight:bold">T</span>)<span style="color:#bbb"> </span>-&gt; <span style="color:#b06;font-weight:bold">Number</span><span style="color:#bbb">
</span></span></span><span style="display:flex;"><span><span style="color:#bbb"></span><span style="color:#080;font-weight:bold">where</span><span style="color:#bbb">
</span></span></span><span style="display:flex;"><span><span style="color:#bbb"> </span>T: <span style="color:#038">Into</span>&lt;Number&gt;,<span style="color:#bbb">
</span></span></span><span style="display:flex;"><span><span style="color:#bbb"></span>{<span style="color:#bbb">
</span></span></span><span style="display:flex;"><span><span style="color:#bbb"> </span><span style="color:#080;font-weight:bold">let</span><span style="color:#bbb"> </span>number<span style="color:#bbb"> </span>=<span style="color:#bbb"> </span>n.into();<span style="color:#bbb"> </span><span style="color:#888">// Conversion is done by the method itself
</span></span></span><span style="display:flex;"><span><span style="color:#888"></span><span style="color:#bbb"> </span>Number(number.<span style="color:#00d;font-weight:bold">0</span><span style="color:#bbb"> </span>+<span style="color:#bbb"> </span><span style="color:#00d;font-weight:bold">1</span>)<span style="color:#bbb">
</span></span></span><span style="display:flex;"><span><span style="color:#bbb"></span>}<span style="color:#bbb">
</span></span></span><span style="display:flex;"><span><span style="color:#bbb">
</span></span></span><span style="display:flex;"><span><span style="color:#bbb"></span><span style="color:#080;font-weight:bold">impl</span><span style="color:#bbb"> </span><span style="color:#038">From</span>&lt;<span style="color:#888;font-weight:bold">u64</span>&gt;<span style="color:#bbb"> </span><span style="color:#080;font-weight:bold">for</span><span style="color:#bbb"> </span>Number<span style="color:#bbb"> </span>{<span style="color:#bbb">
</span></span></span><span style="display:flex;"><span><span style="color:#bbb"> </span><span style="color:#080;font-weight:bold">fn</span> <span style="color:#06b;font-weight:bold">from</span>(value: <span style="color:#888;font-weight:bold">u64</span>)<span style="color:#bbb"> </span>-&gt; <span style="color:#b06;font-weight:bold">Self</span><span style="color:#bbb"> </span>{<span style="color:#bbb">
</span></span></span><span style="display:flex;"><span><span style="color:#bbb"> </span>Self(value)<span style="color:#bbb">
</span></span></span><span style="display:flex;"><span><span style="color:#bbb"> </span>}<span style="color:#bbb">
</span></span></span><span style="display:flex;"><span><span style="color:#bbb"></span>}<span style="color:#bbb">
</span></span></span><span style="display:flex;"><span><span style="color:#bbb">
</span></span></span><span style="display:flex;"><span><span style="color:#bbb"></span><span style="color:#080;font-weight:bold">fn</span> <span style="color:#06b;font-weight:bold">main</span>()<span style="color:#bbb"> </span>{<span style="color:#bbb">
</span></span></span><span style="display:flex;"><span><span style="color:#bbb"> </span><span style="color:#888">// Just because we have implemented the From&lt;u64&gt; do not make the conversion
</span></span></span><span style="display:flex;"><span><span style="color:#888"></span><span style="color:#bbb"> </span><span style="color:#888">// from 10 to Number implicit. Below line do not compile.
</span></span></span><span style="display:flex;"><span><span style="color:#888"></span><span style="color:#bbb"> </span><span style="color:#080;font-weight:bold">let</span><span style="color:#bbb"> </span>num<span style="color:#bbb"> </span>=<span style="color:#bbb"> </span>increment(<span style="color:#00d;font-weight:bold">10</span>);<span style="color:#bbb">
</span></span></span><span style="display:flex;"><span><span style="color:#bbb"> </span><span style="color:#888">// You have to explicitly request the conversion by calling .into()
</span></span></span><span style="display:flex;"><span><span style="color:#888"></span><span style="color:#bbb"> </span><span style="color:#080;font-weight:bold">let</span><span style="color:#bbb"> </span>num<span style="color:#bbb"> </span>=<span style="color:#bbb"> </span>increment(<span style="color:#00d;font-weight:bold">10.</span>into());<span style="color:#bbb">
</span></span></span><span style="display:flex;"><span><span style="color:#bbb"> </span><span style="color:#888">// Or even better, make the increment2 method accept any type that can be
</span></span></span><span style="display:flex;"><span><span style="color:#888"></span><span style="color:#bbb"> </span><span style="color:#888">// converted into a Number. Since u64 can be converted into Number (as it
</span></span></span><span style="display:flex;"><span><span style="color:#888"></span><span style="color:#bbb"> </span><span style="color:#888">// implements From&lt;u64&gt;), increment2() can call the .into() method itself to
</span></span></span><span style="display:flex;"><span><span style="color:#888"></span><span style="color:#bbb"> </span><span style="color:#888">// perform the conversion. At the call site, we simply pass the u64 as a
</span></span></span><span style="display:flex;"><span><span style="color:#888"></span><span style="color:#bbb"> </span><span style="color:#888">// parameter.
</span></span></span><span style="display:flex;"><span><span style="color:#888"></span><span style="color:#bbb"> </span><span style="color:#080;font-weight:bold">let</span><span style="color:#bbb"> </span>num<span style="color:#bbb"> </span>=<span style="color:#bbb"> </span>increment2(<span style="color:#00d;font-weight:bold">10</span>);<span style="color:#bbb">
</span></span></span><span style="display:flex;"><span><span style="color:#bbb"></span>}<span style="color:#bbb">
</span></span></span></code></pre></div>
</main>

<footer>

<script defer src="/js/center-img.js"></script>








<hr/>
© Vineel Kumar Reddy Kovvuri 2017 &ndash; 2025 ❤️ <a href="https://xmin.yihui.org/">Hugo Xmin</a>

</footer>
</body>
</html>

2 changes: 1 addition & 1 deletion docs/blog/rust-from-vs-into-traits/index.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<!DOCTYPE html>
<html lang="en-us">
<head><script src="/livereload.js?mindelay=10&amp;v=2&amp;port=9999&amp;path=livereload" data-no-instant defer></script>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Rust: From vs Into traits | Vineel Kovvuri</title>
Expand Down
2 changes: 1 addition & 1 deletion docs/blog/rust-sharing-objects/index.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<!DOCTYPE html>
<html lang="en-us">
<head><script src="/livereload.js?mindelay=10&amp;v=2&amp;port=9999&amp;path=livereload" data-no-instant defer></script>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Rust: Sharing a Single Object Across Multiple Owners | Vineel Kovvuri</title>
Expand Down
Loading

0 comments on commit deb4a32

Please sign in to comment.