Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
SupperZum authored Dec 19, 2023
1 parent eee701b commit c50df38
Showing 1 changed file with 7 additions and 29 deletions.
36 changes: 7 additions & 29 deletions 2_idioms/2_4_generic_in_type_out/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ The common and obvious rules in [Rust] when choosing type for an input parameter

Let's illustrate it with the following trivial examples:
```rust
use derive_more::{AsRef, AsMut};

// Read-only access is enough here.
pub fn just_print_stringy(v: &str) {
println!("{}", v)
Expand All @@ -23,22 +25,9 @@ pub fn add_hi(v: &mut String) {
v.push_str(" Hi")
}


#[derive(AsMut, AsRef)]
pub struct Nickname(String);

// Implement AsRef for Nickname
impl AsRef<str> for Nickname {
fn as_ref(&self) -> &str {
&self.0
}
}

// Implement AsMut for Nickname
impl AsMut<String> for Nickname {
fn as_mut(&mut self) -> &mut String {
&mut self.0
}
}

impl Nickname {
// We want to own `nickname` inside `Nickname` value.
Expand All @@ -56,30 +45,19 @@ just_print_stringy(nickname.as_ref());

The most standard way to improve ergonomics here is to __hide type conversions under-the-hood by abstracting over input types__ in our APIs:
```rust
pub fn just_print_stringy<S: AsRef<str>>(v: S) {
use derive_more::{AsRef, AsMut};

pub fn just_print_stringy<S: AsRef<String>>(v: S) {
println!("{}", v.as_ref())
}

pub fn add_hi<S: AsMut<String>>(mut v: S) {
v.as_mut().push_str(" Hi")
}

#[derive(AsMut, AsRef)]
pub struct Nickname(String);

// Implement AsRef for Nickname
impl AsRef<str> for Nickname {
fn as_ref(&self) -> &str {
&self.0
}
}

// Implement AsMut for Nickname
impl AsMut<String> for Nickname {
fn as_mut(&mut self) -> &mut String {
&mut self.0
}
}

impl Nickname {
pub fn new<S: Into<String>>(nickname: S) -> Self {
Self(nickname.into())
Expand Down

0 comments on commit c50df38

Please sign in to comment.