Skip to content

Instantly share code, notes, and snippets.

View lffg's full-sized avatar

Luiz Felipe Gonçalves lffg

View GitHub Profile
use std::{
io::{Cursor, Seek, SeekFrom},
ops::RangeBounds,
};
/// Tries to get a UTF-8 string from the given buffer at the provided range.
fn str_from_utf8_range(buf: &[u8], range: impl RangeBounds<usize>) -> Option<&str> {
buf.get((range.start_bound().cloned(), range.end_bound().cloned()))
.map(|slice| std::str::from_utf8(slice).expect("valid utf8"))
}
@lffg
lffg / asm.md
Last active June 14, 2023 23:40

Basics of RISC-V

  • Notes.
    • See An Introduction to Assembly Programming with RISC-V.
    • ISA (Industry Standard Architecture) is the portion of the architecture which is visible to compiler writers. I.e., a contract.
    • There are 33 general-purpose unprivileged registers:
      • zero
      • t0 ~ t6 for temporary values, which are not to be persisted across calls.
      • s0 ~ s11 for saved values.
  • a0 ~ a7 for arguments and return values.
use std::collections::BTreeMap;
fn group<'a>(data: impl Iterator<Item = (&'a str, u32)>) -> BTreeMap<&'a str, u32> {
data.fold(BTreeMap::new(), |mut map, (date, amount)| {
*map.entry(date).or_default() += amount;
map
})
}
#[test]
function query(label) {
console.log(`(${label}) created 'lazy promise', but not started yet`);
return {
then(r) {
console.log(`(${label}) started!`);
// ... process query
// "return" them:
r([{ data: `result for ${label}` }]);
},
};
macro_rules! cat {
($($str:expr),*) => {{
const LEN: usize = $($str.len() + )* 0;
to_str(&combine::<LEN>(&[$($str),*]))
}};
}
const fn combine<const LEN: usize>(list: &[&'static str]) -> [u8; LEN] {
let mut bytes = [0; LEN];
let mut i = 0;
use std::io;
use std::time::Duration;
use std::{
io::Write,
net::{SocketAddr, ToSocketAddrs, UdpSocket},
str::FromStr,
};
use stun_rs::{
MessageClass, MessageDecoderBuilder, MessageEncoderBuilder, StunMessageBuilder,
attributes::stun::XorMappedAddress, methods::BINDING,