Skip to content

Instantly share code, notes, and snippets.

View BSN4's full-sized avatar
🎯
Focusing

Bader BSN4

🎯
Focusing
View GitHub Profile
@BSN4
BSN4 / c6_test.py
Created October 25, 2025 23:59 — forked from rosmo/c6_test.py
TP-Link X90 Deco API example
from requests.api import request
from Crypto.Cipher import PKCS1_OAEP, PKCS1_v1_5
from Crypto.Cipher import AES
from Crypto.PublicKey import RSA
from Crypto.Util.Padding import pad
from Crypto.Util.number import bytes_to_long
import base64
import requests
import string
import random
@BSN4
BSN4 / collect.rs
Created February 23, 2025 12:45
laravel collection in rust
use std::collections::{HashMap, HashSet};
use std::hash::Hash;
pub struct Collection<'a, T> {
items: Vec<&'a T>,
}
impl<'a, T> Collection<'a, T> {
pub fn new(items: &'a [T]) -> Self {
Self {
@BSN4
BSN4 / rust_async.md
Created January 8, 2025 18:36
Async Rust Practical Patterns and Gotchas

Async Rust Practical Patterns and Gotchas

Struct Patterns

// For shared state across tasks, these are your main tools:
struct MyService {
    // Immutable shared state
    config: Arc<Config>,
    // Mutable shared state
@BSN4
BSN4 / owenership.md
Last active January 8, 2025 18:09
rust non-async ownership

Rust Borrowing and Ownership Rules

Struct Methods

Use &mut self when:

  • Modifying struct fields (e.g., update_score, play_round)
  • Maintaining state between calls

Use &self when:

  • Only reading values (e.g., get_player_move, announce_winner)
@BSN4
BSN4 / async2.rs
Created January 8, 2025 11:17
rust async example
use std::sync::Arc;
use tokio::sync::{broadcast, Mutex};
use tokio::time::{sleep, Duration};
#[derive(Debug)]
struct SharedContext {
counter: i32,
last_updated_by: String,
}
@BSN4
BSN4 / async.rs
Created January 8, 2025 10:37
async rust example
use std::sync::Arc;
use tokio::sync::Mutex;
use tokio::time::{sleep, Duration};
// Our shared context that will be accessed by all workers
#[derive(Debug)]
struct SharedContext {
counter: i32,
last_updated_by: String,
}
@BSN4
BSN4 / rock_paper_scissors2.rs
Created January 2, 2025 01:39
rock_paper_scissors rust v2
use rand::Rng;
use std::io::{self, Write};
#[derive(Debug, Default)]
struct Score {
player: u32,
computer: u32,
}
#[derive(Debug, PartialEq, Clone, Copy)]
@BSN4
BSN4 / rock_paper_scissors.rs
Created January 2, 2025 01:38
rock_paper_rust v1
use rand::Rng;
use std::io::{self, Write};
#[derive(Debug)]
struct Score {
player: u32,
computer: u32,
}
#[derive(Debug, PartialEq, Clone)]
@BSN4
BSN4 / rock_paper_scissors_context.ex
Created November 26, 2024 20:05
rock_paper_scissors_context.ex
defmodule RPS do
defmodule Score, do: defstruct player: 0, computer: 0
defmodule Context do
defstruct [
score: %Score{},
rounds_to_play: 3,
valid_moves: [:rock, :paper, :scissors],
win_conditions: %{rock: :scissors, paper: :rock, scissors: :paper},
io: IO
defmodule RPS do
defmodule Score, do: defstruct player: 0, computer: 0
def play do
%Score{}
|> Stream.iterate(&play_round/1)
|> Enum.at(3)
|> announce_winner()
|> maybe_play_again()
end