Skip to content

Instantly share code, notes, and snippets.

View sahandevs's full-sized avatar
🐵
Working from home

Sahand Akbarzadeh sahandevs

🐵
Working from home
View GitHub Profile
@sahandevs
sahandevs / Cargo.toml
Created April 11, 2023 08:26
regex in shared memory
[package]
name = "regex_shared_memory"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
fork = "0.1.21"
libc = "0.2.141"
struct ItemBuilder<const A: bool, const B: bool>;
impl<const A: bool, const B: bool> ItemBuilder<A, B> {
fn build(self) -> usize
where
Self: HasA + HasB,
{
0
}
}
@sahandevs
sahandevs / compile-time-safe-builder.rs
Last active July 3, 2022 07:19
Compile-time safe Builder pattern / idea: https://github.com/maminrayej
pub mod ItemBuilder {
pub struct ItemBuilder<const A_SET: bool, const C_SET: bool, const F_SET: bool> {
a: Option<usize>,
b: Option<usize>,
c: Option<usize>,
d: Option<usize>,
e: Option<usize>,
f: Option<usize>,
}
@sahandevs
sahandevs / 31-crev.md
Last active June 11, 2022 05:58
crev

Crev is a system for verifying security and reliability of dependencies based on collaborative code reviews. Crev users review source code of packages/libraries/crates, and share their findings with others. Crev then uses Web of Trust select trusted reviews and judge reputation of projects' dependencies.

Crev is language-independent, but the primary implementation is cargo crev for Rust/Cargo crates.

# setup
cargo install cargo-crev
cargo crev trust --level high https://github.com/dpc/crev-proofs
cargo crev repo fetch all
@sahandevs
sahandevs / 30-cargo-duplicates.md
Created June 11, 2022 05:47
`cargo-duplicates` crate

A cargo subcommand for displaying when different versions of a same dependency are pulled in.

$ cargo install cargo-duplicates
$ cargo duplicates
Package    Versions
-------    --------
hex        0.4.3  0.3.2
rand_core  0.6.3  0.5.1
@sahandevs
sahandevs / 29-borrow-trait.md
Created June 11, 2022 05:40
`Borrow` trait

In Rust, it is common to provide different representations of a type for different use cases. For instance, storage location and management for a value can be specifically chosen as appropriate for a particular use via pointer types such as Box<T> or Rc<T>. Beyond these generic wrappers that can be used with any type, some types provide optional facets providing potentially costly functionality. An example for such a type is String which adds the ability to extend a string to the basic str. This requires keeping additional information unnecessary for a simple, immutable string.

Types express that they can be borrowed as some type T by implementing Borrow, providing a reference to a T in the trait’s borrow method. A type is free to borrow as several different types. If it wishes to mutably borrow as the type – allowing the underlying data to be modified, it can additionally implement BorrowMut.

NOTE: If generic code merely needs to work for all types that can provide a reference to related

addEventListener("fetch", (event) => {
event.respondWith(
handleRequest(event.request).catch(
(err) => new Response(err.stack, { status: 500 })
)
);
});
const data = {};
@sahandevs
sahandevs / Cargo.toml
Last active August 22, 2022 13:33
proc_macro_demo
[package]
name = "proc_macro_demo_1"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[lib]
proc-macro = true

You can use pattern matching in function parameters. examples:

struct S { 
    field1: String,
    fields2: usize,
}

fn test_fn(S { field1, .. }: S) {
 println!("{}", field1);

demo

nextest is a next-generation test runner for Rust projects.

  • Clean user interface
  • Up to 60% faster than cargo test
  • Detect flaky tests
  • Partition test runs across several CI jobs

QuickStart