GitHub user huang12zheng closed a discussion: can we use AsRef<[u8]> instead of 
&str

* If I want path or key to be [u8], for example in rocksdb, I don't know how to 
do it.
```rust
/// rocksdb
pub fn get<K: AsRef<[u8]>>(&self, key: K) -> Result<Option<Vec<u8>>, Error> {
    self.get_opt(key.as_ref(), &ReadOptions::default())
}

pub fn delete<K: AsRef<[u8]>>(&self, key: K) -> Result<(), Error> {
    self.delete_opt(key.as_ref(), &WriteOptions::default())
}

pub fn put<K, V>(&self, key: K, value: V) -> Result<(), Error>
where
    K: AsRef<[u8]>,
    V: AsRef<[u8]>,
{
    self.put_opt(key.as_ref(), value.as_ref(), &WriteOptions::default())
}
```
* str can be converted to [u8], but [u8] may not be converted to str.
```rust
#[stable(feature = "rust1", since = "1.0.0")]
impl AsRef<[u8]> for str {
```

```rust
pub const fn from_utf8(v: &[u8]) -> Result<&str, Utf8Error> {
    // FIXME: This should use `?` again, once it's `const`
    match run_utf8_validation(v) {
        Ok(_) => {
            // SAFETY: validation succeeded.
            Ok(unsafe { from_utf8_unchecked(v) })
        }
        Err(err) => Err(err),
    }
}
```

* more info in opendal
```rust
pub async fn write(&self, path: &str, bs: impl Into<Bytes>) -> Result<()> {
pub async fn read(&self, path: &str) -> Result<Vec<u8>> {
```

GitHub link: https://github.com/apache/incubator-opendal/discussions/2738

----
This is an automatically sent email for [email protected].
To unsubscribe, please send an email to: [email protected]

Reply via email to