惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园_首页
云风的 BLOG
云风的 BLOG
T
Tailwind CSS Blog
IT之家
IT之家
V
Visual Studio Blog
S
SegmentFault 最新的问题
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Cyberwarzone
Cyberwarzone
T
Tor Project blog
Last Week in AI
Last Week in AI
NISL@THU
NISL@THU
L
Lohrmann on Cybersecurity
V
V2EX
小众软件
小众软件
博客园 - 【当耐特】
S
Schneier on Security
酷 壳 – CoolShell
酷 壳 – CoolShell
Spread Privacy
Spread Privacy
雷峰网
雷峰网
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
K
Kaspersky official blog
大猫的无限游戏
大猫的无限游戏
H
Heimdal Security Blog
N
News and Events Feed by Topic
Know Your Adversary
Know Your Adversary
Apple Machine Learning Research
Apple Machine Learning Research
Forbes - Security
Forbes - Security
博客园 - Franky
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
美团技术团队
S
Securelist
有赞技术团队
有赞技术团队
Engineering at Meta
Engineering at Meta
Simon Willison's Weblog
Simon Willison's Weblog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
U
Unit 42
Scott Helme
Scott Helme
GbyAI
GbyAI
N
Netflix TechBlog - Medium
Recent Commits to openclaw:main
Recent Commits to openclaw:main
P
Privacy International News Feed
P
Proofpoint News Feed
Schneier on Security
Schneier on Security
L
LangChain Blog
Latest news
Latest news
Microsoft Azure Blog
Microsoft Azure Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Y
Y Combinator Blog
L
LINUX DO - 热门话题

oida.dev | TypeScript, Rust

TypeScript's `erasableSyntaxOnly` Flag Unsafe for work Tokio: Macros Tokio: Channels Tokio: Getting Started Network Applications on the Tokio Stack Remake, Remodel, Reduce. The `never` type and error handling in TypeScript 5 Inconvenient Truths about TypeScript Refactoring in Rust: Abstraction with the Newtype Pattern Announcing the TypeScript Cookbook TypeScript: Iterating over objects The road to universal JavaScript 10 years of oida.dev Rust: Tiny little traits The TypeScript converging point How not to learn TypeScript Getting started with Rust Introducing Slides and Coverage TypeScript: The humble function overload TypeScript + React: Children types are broken TypeScript: In defense of any Rust: Enums to wrap multiple errors Dissecting Deno Error handling in Rust TypeScript: Unexpected intersections Upgrading Node.js dependencies after a yarn audit TypeScript: Array.includes on narrow types TypeScript + React: Typing Generic forwardRefs shared, util, core: Schroedinger's module names Learning Rust and Go TypeScript: Narrow types in catch clauses TypeScript: Low maintenance types Tidy TypeScript: Name your generics Tidy TypeScript: Avoid traditional OOP patterns Tidy TypeScript: Prefer type aliases over interfaces Tidy TypeScript: Prefer union types over enums My new book: TypeScript in 50 Lessons Go Preact! ❤️ this in JavaScript and TypeScript TypeScript and ECMAScript Modules TypeScript + React: Why I don't use React.FC TypeScript + React: Component patterns TypeScript: Augmenting global and lib.dom.d.ts Vite with Preact and TypeScript TypeScript: Union to intersection type 11ty: Generate Twitter cards automatically Are large node module dependencies an issue? TypeScript: Variadic Tuple Types Preview TypeScript: Improving Object.keys Remake, Remodel. Part 4. TypeScript + React: Typing custom hooks with tuple types TypeScript: Assertion signatures and Object.defineProperty TypeScript: Check for object properties and narrow down type Boolean in JavaScript and TypeScript void in JavaScript and TypeScript Symbols in JavaScript and TypeScript Why I use TypeScript TypeScript + React: Extending JSX Elements TypeScript: Validate mapped types and const context TypeScript: Match the exact object shape TypeScript: The constructor interface pattern Streaming your Meetup - Part 4: Directing and Streaming with OBS Streaming your Meetup - Part 3: Speaker audio Streaming your Meetup - Part 2: Speaker video Streaming your Meetup - Part 1: Basics and Projector TypeScript and React Guide: Added a new styles chapter TypeScript and React Guide: Added a new render props chapter TypeScript and React: Styles and CSS TypeScript and React TypeScript and React Guide: Added a new prop types chapter TypeScript without TypeScript -- JSDoc superpowers TypeScript: Mapped types for type maps JAMStack vs serverless web apps The Unsung Benefits of JAMStack Sites TypeScript: Ambient modules for Webpack loaders My most favourite talks in 2018 TypeScript and React Guide: Added a new context chapter TypeScript: Built-in generic types TypeScript: Type predicates JSX is syntactic sugar TypeScript and React Guide: Added a new hooks chapter Getting your CfP application right FAQ on our Angular Connect Talk: Automating UI development TypeScript and Substitutability Debugging Node.js apps in TypeScript with Visual Studio Code From Medium: Deconfusing Pre- and Post-processing From Medium: PostCSS misconceptions Saving and scraping a website with Puppeteer Cutting the mustard - 2018 edition Wordpress as CMS for your JAMStack sites My most favourite podcast episodes in 2017 My most favourite talks in 2017 My most favourite books in 2017 The Best Request Is No Request, Revisited Not so hidden figures - Organizing ScriptConf My podcast journey to ScriptCast Grid layout, grid layout everywhere! #scriptconf and #devone Object streams in Node.js
Refactoring in Rust: Introducing Traits
2023-03-02 · via oida.dev | TypeScript, Rust

In the same codebase as last time, we extract data from a HashMap<String, String> called headers, presumably dealing with something similar to HTTP headers.

if headers.contains_key("x-user-id") {
headers
.get("x-user-id")
.map(ToOwned::to_owned)
.unwrap()
}

Again, for a few lines of code, there is a lot going on here. We check if a certain property is available, and if that’s the case we take an owned value from the hash map. get returns an Option<&String>, but since we did a contains_key check first, we can be sure that the unwrap() call won’t panic.

This procedure occurs a few times in our codebase. Sometimes together with some sort of error handling, which indicates that that "x-user-id" is a required header.

let user_id = if headers.contains_key("x-user-id") {
headers
.get("x-user-id")
.map(ToOwned::to_owned)
.unwrap()
} else {
return Err(HeaderError {});
}

HeaderError is a custom error, we like to hold on to that for later.

use std::fmt::{Result, Formatter};

#[derive(Debug)]
struct HeaderError;

impl Display for HeaderError {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
write!(f, "Header not found")
}
}

impl std::error::Error for HeaderError {}

Sometimes we don’t unwrap the output from headers.get. This tells us that this header is optional.

if headers.contains_key("x-authentication") {
headers
.get("x-authentication")
.map(ToOwned::to_owned)
}

It works, which is good, but there are some hidden problems that might bite us. The code itself doesn’t tell us anything about what we expect to happen. Knowing that we deal with optional or required headers is something that we mostly know because, well, I told you so a few paragraphs earlier.

Also, the unwrap() call is something that sticks out like a sore thumb. Yes, it should be safe and not panic because we did a conditional check on the existence of a key earlier, but since it’s not clearly defined what this code is supposed to do it’s just too easy for somebody to get rid of this condition at a later stage.

And last, we check for the existence of a key twice. Once when we explicitly ask if the headers struct contains a certain key, and then again through the return type of headers.get. headers.get returns an Option<&String>. This type tells us everything there is to know: This call might yield no value.

Most of the problems can be addressed by introducing context and semantics. The easiest way to do so is by introducing a new type. Let’s call this type Headers, and let it wrap HashMap<String, String>.

struct Headers(HashMap<String, String>);

impl Headers {
pub fn new(headers: HashMap<String, String>) -> Self {
Self(headers)
}

pub fn get_required_header(&self, name: &str)
-> Result<String, HeaderError> {
self.0
.get(name)
.map(ToOwned::to_owned)
.ok_or(HeaderError {})
}

pub fn get_optional_header(&self, name: &str) -> Option<String> {
self.0.get(name).map(ToOwned::to_owned)
}
}

We introduce two methods:

  • One is called get_required_header. It errors when a header is not available, which we can see immediately from the return type.
  • The other is called get_optional_header. It returns the same as headers.get, but an owned value instead of a reference.

In both cases, we only need to look at the method signature to understand what’s going on. The method name gives us context, the return type tells us what to expect. We also got rid of any conditionals that might surround the actual task and are able to properly use error propagation, unwrap_or_default, or similar. The characteristics of the data structure move to the back, and our goals of what we want to achieve with them come forward.

There is just one thing that is odd about this implementation. The functionality we just described, the semantics of our program, are more or fewer convenience methods for an already existing data structure. Shouldn’t we wish that our original data structure would have those characteristics, or dare I say it … traits?

Instead of a new type, we introduce a trait called Headers that describes the methods we just implemented earlier. It tells us everything we need to know. This is what we care about.

trait Headers {
fn get_required_header(&self, name: &str) -> Result<String, HeaderError>;
fn get_optional_header(&self, name: &str) -> Option<String>;
}

For our function, we only want to get something that is impl Headers and implements the right methods. The actual data structure is now completely irrelevant to the semantics of our software. If those methods are associated with a HashMap is an implementation detail for some other part of our program.

Speaking of the implementation, for hashmaps, it’s almost the same as it was before.

impl Headers for HashMap<String, String> {
fn get_required_header(&self, name: &str) -> Result<String, HeaderError> {
self.get(name).map(ToOwned::to_owned).ok_or(HeaderError {})
}

fn get_optional_header(&self, name: &str) -> Option<String> {
self.get(name).map(ToOwned::to_owned)
}
}

Using implemented Headers is a delight. When we look at the following lines of code we see which parts are required, and which ones are optional, and we know how to handle errors and stay on the happy path.

fn do_something(headers: &impl Headers) -> Result<(), HeaderError> {
let user_id = headers.get_required_header("x-user-id")?;
let token = headers.get_required_header("x-token")?;
let tenant_id = headers.get_optional_header("x-tenant");

// ...
Ok(())
}

It’s also a lot less for us to parse and understand. We don’t hide semantics by looking at basic data types and control flow, the code tells us what’s up. Nothing to keep in our mind maps, everything is laid out and spelled out clearly. Every intention is visible.

We also stay flexible with implementations. Imagine that headers will become optional at some point in time, but you still need to get header information at some other part of your code. Implement the same trait for Option<HashMap<String, String>> and don’t change a single thing of your actual logic.

impl Headers for Option<HashMap<String, String>> {
fn get_required_header(&self, name: &str) -> Result<String, HeaderError> {
match self {
Some(headers_map) => headers_map.get_required_header(name),
None => Err(HeaderError {}),
}
}

fn get_optional_header(&self, name: &str) -> Option<String> {
self.as_ref()
.and_then(|headers_map| headers_map.get_optional_header(name))
}
}

You might argue that a new type might be more fitting since not every HashMap<String, String> might contain header information. And that’s true! If you expose your traits to other parts of your software or have other hash maps with the same format around, names might be confusing.

Since refactoring is a lot about naming things, you might want to opt for something more generic, like a StringMapExt that does the required/optional handling for you.

trait StringMapExt {
fn get_required_value(&self, name: &str) -> Result<String, Unavailable>;
fn get_optional_value(&self, name: &str) -> Option<String>;
}

impl StringMapExt for HashMap<String, String> {
fn get_required_value(&self, name: &str) -> Result<String, Unavailable> {
self.get(name).map(ToOwned::to_owned).ok_or(Unavailable {})
}

fn get_optional_value(&self, name: &str) -> Option<String> {
self.get(name).map(ToOwned::to_owned)
}
}

Introducing traits when refactoring is a fantastic way to state your intentions. They make your code more readable and maintainable, and won’t keep you busy parsing built-in types.

Related Articles