favicon here hometagsblogmicrobio cvtech cvgpg keys

Cow is weird

#rust

uncomfyhalomacro | 2024-06-24 | reading time: ~1min

So I just found out something weird while learning and writing a tutorial for Rust,

    let body: &std::borrow::Cow<'_, str> = http_req.body();
    if buf_str.contains(&**body) {
        println!("Body is same in buf");
    }

So I found out that &**body is possible because

body() -> &Cow -> deref -> Cow -> deref -> str (Size cannot be known at compile time)

therefore, add a reference -> & -> &str

OR

we can just use .deref and since Cow already has Deref trait, it's actually a "double" deref like the previous explanation. However, there is a slight difference

OR

we can also just use .as_ref since Cow has Deref trait and the .as_ref method will reference the inferred type T self which is String but since it's a ref so it's &String which is then coerced to &str because of String's Deref trait. See https://doc.rust-lang.org/stable/src/alloc/string.rs.html#2683 and https://doc.rust-lang.org/stable/src/alloc/string.rs.html#2479

Articles from blogs I follow around the net

Status update, November 2024

Hi all! This month I’ve spent a lot of time triaging Sway and wlroots issues following the Sway 1.10 release. There are a few regressions, some of which are already fixed (thanks to all contributors for sending patches!). Kenny has added support for softwa…

via emersionNovember 21, 2024

Key Transparency and the Right to be Forgotten

This post is the first in a new series covering some of the reasoning behind decisions made in my project to build end-to-end encryption for direct messages on the Fediverse. (Collectively, Fedi-E2EE.) Although the reasons for specific design decisions sho…

via Dhole MomentsNovember 21, 2024

The Ethereum MEV Bots are a scam

Several months ago, I wrote this article: The Fascinating Ethereum MEV Bot Scam, where I dived deep into the technical aspects of a scam that has been circulating for years on platforms like X.com and YouTube. These scams revolve around deploying smart…

via Christian Visintin BlogNovember 19, 2024

hyper in curl Needs a Champion

tl;dr - hyper in curl is nearly complete, but it needs a champion. Without a partner actively engaged that wants to enable and ship, it’s now on the path for being deprecated and removed. It needs a champion, a backing vendor or distro. Will that be you? …

via seanmonstarNovember 19, 2024

Swift observations from a reluctant Rustacean

Recently I've been thinking about Swift in terms of Rust, & have appreciated anew some of the choices made. In Rust. There's been a proliferation of X vs Y posts on the web, especially since the advent of LLM AI, so I try to resist the format. It's often e…

via Mike KreuzerNovember 16, 2024

Go Concurrency Patterns

Goroutines Channels Select Statement Wait Groups Mutex Conditions Atomic Operations Once Context Map Real Examples Goroutines The go keyword is used to start a goroutine. A goroutine is a lightweight, managed thread used by the Go runtime to run functions …

via Posts on integralistNovember 15, 2024

Generated by openring-rs

favicon here hometagsblogmicrobio cvtech cvgpg keys