0

I am trying to implement a flexible type system at runtime in Rust. This is what I have that works so far:

use std::borrow::Cow;

pub struct Float {
    pub min: f64,
    pub max: f64,
    pub value: f64,
}

pub struct Text<'a> {
    pub value: Cow<'a, str>
}

pub enum Value<'a> {
    None,
    Float(Float),
    Text(Text<'a>),
}

This works as I want it to, and now I want a vector of itself (and a map would be the next step), so I added:

pub struct Vector<'a> {
    pub value: Cow<'a, Vec<Value<'a>>>,
}

And extended the enum to:

pub enum Value<'a> {
    None,
    Float(Float),
    Text(Text<'a>),
    Vector(Vector<'a>),
}

Now I get an error message:

error[E0277]: the trait bound `Value<'a>: std::clone::Clone` is not satisfied
  --> src/lib.rs:14:5
   |
14 |     pub value: Cow<'a, Vec<Value<'a>>>,
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::clone::Clone` is not implemented for `Value<'a>`
   |
   = note: required because of the requirements on the impl of `std::clone::Clone` for `std::vec::Vec<Value<'a>>`
   = note: required because of the requirements on the impl of `std::borrow::ToOwned` for `std::vec::Vec<Value<'a>>`
   = note: required by `std::borrow::Cow`

error[E0277]: the trait bound `Value<'a>: std::clone::Clone` is not satisfied
  --> src/lib.rs:21:12
   |
21 |     Vector(Vector<'a>),
   |            ^^^^^^^^^^ the trait `std::clone::Clone` is not implemented for `Value<'a>`
   |
   = note: required because of the requirements on the impl of `std::clone::Clone` for `std::vec::Vec<Value<'a>>`
   = note: required because of the requirements on the impl of `std::borrow::ToOwned` for `std::vec::Vec<Value<'a>>`
   = note: required because it appears within the type `Vector<'a>`
   = note: no field of an enum variant may have a dynamically sized type

I tried several ways to implement Clone, but as a beginner I just ended up with countless other error messages. How do I get a vector of Value into this system?

Why do I do this?

I have the following code to simplify the usage of Value:

impl<'a> Value<'a> {
    pub fn float_val(&self) -> f64 {
        match self {
            Value::None => 0.0,
            Value::Float(f) => f.value,
            Value::Text(t) => t.value.parse().unwrap_or(0.0),
        }
    }

    pub fn str_val(&'a self) -> Cow<'a, str> {
        match self {
            Value::None => Cow::Owned("".to_string()),
            Value::Float(f) => Cow::Owned(f.value.to_string()),
            Value::Text(t) => Cow::Borrowed(&t.value),
        }
    }
}

This allows me to use it in functions like:

fn append_string(s1: &Value, s2: &Value) {
    Value::Text(Text {
        value: format!("{}{}", s1.str_val(), s2.str_val()),
    })
}

I want the same for a vector, which I assume would be like:

pub fn vec(&'a self) -> Vec<Value> {
    match self {
        Value::Vector(v) => v.value,
        _ => Cow::Owned(Vector { value: vec![] }),
    }
}
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Hellagot
  • 235
  • 2
  • 13
  • Why does `value` need to be a `Cow` here? – Peter Hall Sep 15 '18 at 11:53
  • The reason is, that i provide functions like: fn to_vec(&self), where it returns the original vector, if its a vector, or an empty vector if Value is for example a Float – Hellagot Sep 15 '18 at 12:20
  • Add `#[derive(Clone)]` to all your structs and the enum. Then you get a different error (`error[E0275]: overflow evaluating the requirement 'Value<'a>: std::marker::Sized'`), which seems to me to be a compiler bug, carelessness w.r.t. recursive definitions. – starblue Sep 15 '18 at 12:27
  • tried to explain why i'm doing this, @starblue yes i tried that, same error message – Hellagot Sep 15 '18 at 12:30
  • 2
    @Hellagot I believe you can have functions that return `Cow`s without having to *store* `Cow`s in `Value`. Using a `Cow` in a struct/enum lets you store either an owned or a borrowed value; do you really need that? What about using `Rc` or `Arc` instead? – Francis Gagné Sep 15 '18 at 12:31
  • 3
    @Hellagot `Cow<'a, Vec>>` is a bit weird. You probably _want_ Cow<'a, [Value<'a>]>`, which is a slice when borrowed and a `Vec` when owned. – Peter Hall Sep 15 '18 at 12:32
  • 1
    The root of the issue though is that `Cow` requires that `T: ToOwned` in order to create an owned variant. The reason it's suggesting `Clone` is because there is a blanket implementation of `ToOwned` for all types that are `Clone`. But this isn't necessarily what you want – Peter Hall Sep 15 '18 at 12:37
  • 1
    If you try to implement `ToOwned` for `Value` or `[Value]` yourself, you'll find yourself needing to make a clone somewhere. – Peter Hall Sep 15 '18 at 13:01
  • 1
    Also, with a recursive type, you usually need to introduce some indirection via a `Box` or similar. `Cow` doesn't serve this purpose because it only _sometimes_ provides indirection (when it's `Borrowed`). – Peter Hall Sep 15 '18 at 13:23
  • 2
    @PeterHall Don't `Vec` and `[]` provide that indirection here? – Francis Gagné Sep 15 '18 at 13:24
  • @FrancisGagné Oh yes, that's true. – Peter Hall Sep 15 '18 at 13:27

1 Answers1

3

I want the same for a vector, which I assume would be like:

pub fn vec(&'a self) -> Vec<Value> {
match self {
        Value::Vector(v) => v.value,
        _ => Cow::Owned(Vector { value: vec![] }),
    }
}

First of all, to return a Cow from a function doesn't mean you have to store your data as a Cow. You can do this:

pub struct Vector<'a> {
    pub value: Vec<Value<'a>>,
}

pub enum Value<'a> {
    None,
    Float(Float),
    Text(Text<'a>),
    Vector(Vector<'a>),
}

And then your to_vec would look like this:

impl<'a> Value<'a> {
    pub fn to_vec(&'a self) -> Cow<'a, [Value<'a>]> {
        match self {
            Value::Vector(v) => Cow::Borrowed(&v.value),
            _ => Cow::Owned(Vec::new()),
        }
    }
}

Except that you still will have some problems with implementing ToOwned for Value<'a>, so this won't immediately work.

However, I don't see why a Cow is necessary here anyway. Cow abstracts over a borrowed vs owned type, but your owned value is always empty, so what's the harm in returning a borrowed slice in both cases? It would look like this:

impl<'a> Value<'a> {
    pub fn to_vec_slice(&'a self) -> &'a [Value<'a>] {
        match self {
            Value::Vector(v) => &v.value,
            _ => &[],
        }
    }
}
Peter Hall
  • 53,120
  • 14
  • 139
  • 204
  • 1
    `Cow<'a, [Value<'a>]>` and `&'a [Value<'a>]` are both a little weird here because they unnecessarily tie the lifetime of the returned reference to `'a`. In both cases I would create a fresh lifetime, or allow the compiler to elide it: `fn to_vec(&self) -> Cow<'_, [Value<'a>]>` and `fn to_vec(&self) -> &[Value<'a>]` – trent Sep 15 '18 at 17:04
  • 1
    Although, I guess unifying the lifetimes could be intentional? It will work if everything's immutable. – trent Sep 15 '18 at 17:10
  • @trentcl No, it was just a bit lazy on my part. – Peter Hall Sep 15 '18 at 17:30
  • That could still be what OP wants. Hard to tell for sure. OP's `str_val` is suspicious for the same reason. – trent Sep 15 '18 at 17:32
  • Thank you all, the slice version is what i was looking for, still learning. At the moment i want to keep the source values immutable, but i will try to remove the returned lifetimes where possible to let the compiler elide it, as suggested by @trentcl – Hellagot Sep 16 '18 at 04:40