Skip to content

Instantly share code, notes, and snippets.

View sam0x17's full-sized avatar

Sam Johnson sam0x17

View GitHub Profile
@sam0x17
sam0x17 / keybase.md
Created November 30, 2023 21:48
keybase.md

Keybase proof

I hereby claim:

  • I am sam0x17 on github.
  • I am sam0x17 (https://keybase.io/sam0x17) on keybase.
  • I have a public key ASD59tnchzvt33Gvmfz_J-ooAwmgcVE5dopA3Nkd9RgNrwo

To claim this, I am signing this object:

@sam0x17
sam0x17 / evil_rust.rs
Created August 18, 2023 06:05
evil rust
trait RawBytes: Sized {
fn raw_bytes(&self) -> &[u8] {
let ptr = self as *const Self as *const u8;
unsafe { core::slice::from_raw_parts(ptr, std::mem::size_of::<Self>()) }
}
}
impl<T> RawBytes for T {}
@sam0x17
sam0x17 / question_closure_trick.rs
Created July 26, 2023 15:49
Rust workaround for "the `?` operator can only be used in a closure that returns `Result` or `Option`" when trying to use `?` inside a closure inside a `.map`
let const_fns: Vec<TraitItemFn> = const_fns
.into_iter()
.map(|item| match item {
TraitItem::Fn(trait_item_fn) => Ok(*trait_item_fn),
_ => return Err(Error::new(item.span(), "expected `fn`")),
})
.collect::<std::result::Result<_, Self::Error>>()?;
@sam0x17
sam0x17 / const_str_eq.rs
Created July 20, 2023 15:59
Rust const_str_eq
pub const fn const_str_eq(a: &str, b: &str) -> bool {
if a.as_bytes().len() != b.as_bytes().len() {
return false;
}
let mut i = 0;
while i < a.as_bytes().len() {
if a.as_bytes()[i] != b.as_bytes()[i] {
return false;
}
i += 1;
@sam0x17
sam0x17 / assert_impl.rs
Last active July 4, 2023 03:58
rust assert_impl!
macro_rules! assert_impl {
($typ:ty, $($trait:path),*$(,)?) => {
{
const fn _assert_impl<T>()
where
T: $($trait +)* ?Sized,
{}
_assert_impl::<$typ>();
}
}
@sam0x17
sam0x17 / modules_in_rust.md
Last active October 12, 2023 15:31
Cheat sheet / beginner guide for how modules work in Rust
mod some_mod;

Simply means "there exists some file called some_mod.rs in this directory, or there exists some directory called some_mod in this directory that contains a file called mod.rs. Typing this line will make whatever content contained within the file (some_mod/mod.rs or some_mod.rs) available in this file via some_mod::whatever.

So Rust source files are modules. There is also a handy inline syntax for defining a short sub-module in the current file/module without needing to have a separate file for this module. This syntax is the source of confusion for a lot of people as it makes them think they have to write mod some_mod { to define any modules. Nope, this is just an inline shortcut for making a separate file and putting stuff in there, like the following:

mod some_other_mod {
 // stuff in here
@sam0x17
sam0x17 / generic_str.rs
Created May 23, 2023 13:29
Rust trait generic over all types of string references
pub trait GenericStr {
fn as_str_generic(&self) -> &str;
}
impl GenericStr for &String {
fn as_str_generic(&self) -> &str {
self.as_str()
}
}
@sam0x17
sam0x17 / dev_setup.sh
Last active May 1, 2023 20:49
dev environment
#!/bin/bash
set -ex
# run updates, clean unneeded packages
sudo apt-get update --assume-yes
sudo apt-get upgrade --assume-yes
sudo apt-get remove --assume-yes libreoffice-*
# fix repo issue
sudo add-apt-repository -r ppa:ubuntu-toolchain-r/test
[build]
rustc-wrapper = "/home/sam/.cargo/bin/sccache"
[profile.rust-analyzer]
inherits = "dev"
@sam0x17
sam0x17 / rust_analyzer_separate_build_dir.md
Last active May 1, 2023 20:25
new solution for getting separate workspace/project-specific build directory for rust-analyzer

The following will allow you to automatically have a workspace-specific separate build directory for rust-analyzer:

in userSettings.json (vscode):

"rust-analyzer.server.extraEnv": {
  "CARGO_TARGET_DIR": "target/analyzer"
},
"rust-analyzer.check.extraArgs": [
  "--target-dir=target/analyzer"
]