Skip to content

Instantly share code, notes, and snippets.

View nanoqsh's full-sized avatar
🌑
I wish to live in a better world

Nano nanoqsh

🌑
I wish to live in a better world
View GitHub Profile
@nanoqsh
nanoqsh / name.rs
Last active March 31, 2024 02:13
Small limited alloc free string
use {
serde::{de, Deserialize, Serialize},
std::{error, fmt, hint, num::NonZeroU128, slice, str},
};
/// Small limited alloc free string.
///
/// The name has the same layout as `&str` and also has a niche.
///
/// # Invariants
@nanoqsh
nanoqsh / conv.rs
Last active June 26, 2023 16:56
Simple image converter
use {
clap::Parser,
image::{io::Reader, ImageOutputFormat},
std::{
error,
io::{self, Cursor, Read, Write},
},
};
type Error = Box<dyn error::Error>;
@nanoqsh
nanoqsh / md.rs
Created May 9, 2023 20:27
Simple markdown parser example
use {
pulldown_cmark::{
escape::{self, StrWrite},
Event, HeadingLevel as Hl, Parser, Tag,
},
std::{
fmt::{self, Arguments},
io::{self, Error, ErrorKind},
},
};
@nanoqsh
nanoqsh / username.rs
Created July 19, 2022 19:31
Get the username in Rust
use std::string::FromUtf8Error;
fn username() -> Result<String, Error> {
use std::{ffi::CStr, mem::MaybeUninit};
const BUFLEN: usize = 256;
unsafe {
let uid = libc::getuid();
let mut pwd: MaybeUninit<libc::passwd> = MaybeUninit::uninit();
@nanoqsh
nanoqsh / format.rs
Created May 3, 2022 01:06
Custom formatting
#![feature(iter_intersperse)]
use std::fmt;
struct Format<T>(pub T);
impl<T: fmt::Display> fmt::Display for Format<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use std::{str, io::{Write, Cursor, ErrorKind}};