Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create newtypes for an unsized type and its owned counterpart (like `str` and `String`) in safe Rust?

Tags:

rust

newtype

I want to create a pair of newtypes Tag(str) and TagBuf(String), analogous to how Path and PathBuf wrap OsStr and OsString. My end goal is to have a map keyed by TagBuf and to be able to index into it with just a Tag:

fn main() {
    let mut m: HashMap<TagBuf, i32> = HashMap::new();
    m.insert(TagBuf("x".to_string()), 1);
    assert_eq!(m.get(Tag::new("x")), Some(&1));
}

But I’m running into issues because Tag is dynamically sized.

Specifically, implementing Borrow<Tag> for TagBuf is tricky:

pub struct Tag(str);
pub struct TagBuf(String);

impl std::borrow::Borrow<Tag> for TagBuf {
    fn borrow(&self) -> &Tag {
        let s: &str = self.0.as_str();
        // How can I turn `&str` into `&Tag`? A naive attempt fails:
        &Tag(*s)
    }
}
error[E0277]: the size for values of type `str` cannot be known at compilation time
 --> src/lib.rs:8:10
  |
8 |         &Tag(*s)
  |          ^^^ doesn't have a size known at compile-time
  |
  = help: the trait `Sized` is not implemented for `str`
  = note: all function arguments must have a statically known size

I can just return unsafe { std::mem::transmute(s) } with a #[repr(transparent)] annotation, but I would like to avoid unsafe code.

I’ve looked at the source for Path/PathBuf and come up with the following:

use std::borrow::Borrow;
use std::ops::Deref;

#[repr(transparent)]
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Tag(str);
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone)]
pub struct TagBuf(String);

impl Tag {
    fn new<S: AsRef<str> + ?Sized>(s: &S) -> &Tag {
        unsafe { &*(s.as_ref() as *const str as *const Tag) }
    }
}

impl Deref for TagBuf {
    type Target = Tag;
    fn deref(&self) -> &Tag {
        Tag::new(&self.0)
    }
}

impl Borrow<Tag> for TagBuf {
    fn borrow(&self) -> &Tag {
        self.deref()
    }
}

impl ToOwned for Tag {
    type Owned = TagBuf;
    fn to_owned(&self) -> TagBuf {
        TagBuf(self.0.to_owned())
    }
}

fn main() {
    let mut m = std::collections::HashMap::<TagBuf, i32>::new();
    m.insert(TagBuf("x".to_string()), 1);
    assert_eq!(m.get(Tag::new("x")), Some(&1));
}

…and this works, and I can understand it (good!), but it still uses unsafe for that cast, which I’d like to avoid.

I saw the Rustonomicon section on exotically sized types, which doesn’t use unsafe, but the unsizing coercion seems complicated, and I don’t see how to adapt it from [u8] to str, since there’s no stringy counterpart to [u8; N].

I also read the implementation of Rc<str>, which seems to do some more unsafe conversion via Rc<[u8]> and some specialization magic that I had trouble understanding.

I’ve read some related questions, like:

  • Implementing FromStr for a custom &[u8] type
  • https://users.rust-lang.org/t/how-to-newtype-string/5211

…but I haven’t found an answer.

Does latest stable Rust have a way to define a newtype pair for str and String in safe code? If not, are there RFCs or tracking issues that I should follow?

like image 492
wchargin Avatar asked Oct 14 '22 23:10

wchargin


1 Answers

This cannot be solved in safe Rust without some small overhead.

This is how I'd solve it using unsafe:

use std::{borrow::Borrow, ops::Deref};

#[repr(transparent)]
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Tag(str);

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone)]
pub struct TagBuf(String);

impl Tag {
    fn new<S: AsRef<str> + ?Sized>(s: &S) -> &Tag {
        unsafe { &*(s.as_ref() as *const str as *const Tag) }
    }
}

impl Deref for TagBuf {
    type Target = Tag;
    
    fn deref(&self) -> &Tag {
        Tag::new(&self.0)
    }
}

impl Borrow<Tag> for TagBuf {
    fn borrow(&self) -> &Tag {
        self.deref()
    }
}

impl ToOwned for Tag {
    type Owned = TagBuf;
    
    fn to_owned(&self) -> TagBuf {
        TagBuf(self.0.to_owned())
    }
}

use std::collections::HashMap;

fn main() {
    let mut m = HashMap::new();
    m.insert(TagBuf("x".to_string()), 1);
    assert_eq!(m.get(Tag::new("x")), Some(&1));
}

See also:

  • How do I handle an FFI unsized type that could be owned or borrowed?
  • How to wrap a borrowed value in a newtype that is also a borrowed value?
  • How to avoid temporary allocations when using a complex key for a HashMap?
  • How to implement HashMap with two keys?
like image 179
Shepmaster Avatar answered Jan 04 '23 07:01

Shepmaster