Assuming I have an enum as:
enum Foo {
A,
B,
}
...where I want to implement something like this:
impl From<char> for Option<Foo> {
fn from(chr: char) -> Self {
match chr {
'A' => Some(Foo::A),
'B' => Some(Foo::B),
_ => None,
}
}
}
This is currently illegal since Option is out of my own crate, it is owned by std. The compiler error is:
only traits defined in the current crate can be implemented for types defined outside of the crate
define and implement a trait or new type instead rustc(E0117)
As in the case I have shown above, in some cases, I'd like to have None instead of Some. Is there a way to implement a From that I am able to get Option<U> instead of U itself?
Environment
Rust 1.62.1
I think what you actually want in this scenario is to implement the TryFrom trait instead.
For example (ideally you'd make a proper Error type rather than a str):
impl TryFrom<char> for Foo {
type Error = &'static str;
fn try_from(chr: char) -> Result<Foo, Self::Error> {
match chr {
'A' => Ok(Foo::A),
'B' => Ok(Foo::B),
_ => Err("can't convert character to Foo"),
}
}
}
Related
I'm attempting to create a struct that holds a collection of Nodes. In order to limit the type, each of these Nodes can hold the value is of the enum type NodeVal.
I can then add accessor functions to the Container struct to get and set the values. However, rather than adding a get_node_f64, get_node_i64, etc, I'm attempting to make a generic function that accepts a type that implements the Num trait.
This does not work seemingly because the val property of Node is NodeVal rather than T. However if I make it T it will be able to be any type, which I want to avoid.
Is there any way to achieve what I want to do or am I structuring this the wrong way?
use std::collections::HashMap;
use num_traits::Num;
pub enum NodeVal {
Str(String),
F64(f64),
Uint64(u64),
Int64(i64),
}
pub struct Node {
id: i32,
val: NodeVal
}
pub struct Container {
nodes: HashMap<i32, Node>
}
impl Container {
pub fn new() -> Self {
Container {
nodes: HashMap::new()
}
}
pub fn get_node_str(&self, key: &i32) -> Option<String> {
match self.nodes.get(key) {
Some(r) => match &r.val {
NodeVal::Str(x) => Some(x.to_string()),
_ => None
},
None => None
}
}
// Does not compile
pub fn get_node_num<T: num_traits::Num>(&self, key: &i32) -> Option<T> {
match self.nodes.get(key) {
Some(r) => match &r.val {
NodeVal::F64(x) | NodeVal::Uint64(x) | NodeVal::Int64(x) => Some(*x),
_ => None
},
None => None
}
}
}
This does not work seemingly because the val property of Node is NodeVal rather than T. However if I make it T it will be able to be any type, which I want to avoid.
What I get is that it doesn't work because x is of a different type in the three variants you're matching, which doesn't make any sense to Rust, it complains that the x in F64 is an f64, the x in Uint64 is an u64 and the x in Int64 is an i64, therefore the type of x makes no sense (it has three incompatible types it can't reconcile).
Your use of trait bounds is also incorrect, trait bounds are a way for the caller to specify types, but get_node_num does not consider that for a single second, it doesn't care what the caller wants.
Plus the reasoning doesn't make sense:
However if I make it T it will be able to be any type, which I want to avoid.
get_node_num decides what the return type is, T is completely useless. get_node_num also can't work, because you can't return a "f64 or u64 or i64" in Rust, except by creating a new enum which stores these alternatives.
I had a use-case where I wanted to store a function in an enum type
#[derive(Debug)]
enum RispErr {
Reason(String),
}
#[derive(Clone)]
enum RispExp {
Bool(bool),
Symbol(String),
Number(f64),
List(Vec<RispExp>),
Func(fn(&Vec<RispExp>) -> Result<RispExp, RispErr>),
}
In one case, I wanted to create a higher order function to generate these
// simplifying this for the question
// shortening this for brevity
fn make_tonicity_checker(
tone_fn: fn(f64, f64) -> bool,
) -> impl Fn(&Vec<RispExp>) -> Result<RispExp, RispErr> {
return move |args: &Vec<RispExp>| -> Result<RispExp, RispErr> {
tone_fn(1.0, 2.0); // need to use this
return Ok(RispExp::Bool(true));
};
}
I come across errors though when I tried to use the higher order function
fn f() -> () {
RispExp::Func(make_tonicity_checker(|a, b| a > b));
}
mismatched types
expected fn pointer, found opaque type
note: expected type `for<'r> fn(&'r std::vec::Vec<RispExp>) -> std::result::Result<RispExp, RispErr>`
found type `impl for<'r> std::ops::Fn<(&'r std::vec::Vec<RispExp>,)>`rustc(E0308)
main.rs(93, 5): expected fn pointer, found opaque type
I dug deeper and realized that function pointers cannot capture the environment, and hence the error. I tried
Func(Fn(&Vec<RispExp>) -> Result<RispExp, RispErr>),
but, realized this fails too, as it doesn't have a size known at compile time. with some googling, I found that I could potentially pass this in as a type parameter
#[derive(Clone)]
enum RispExp<T>
where
T: Fn(&Vec<RispExp<T>>) -> Result<RispExp<T>, RispErr>,
{
Bool(bool),
Symbol(String),
Number(f64),
List(Vec<RispExp<T>>),
Func(T),
}
but then, in all places where I accept RispExp, I would need to provide this type parameter. That seems a bit annoying to do, because I would have to repeat where T: Fn(&Vec<RispExp<T>>) -> Result<RispExp<T>, RispErr>, everywhere.
I could do Func(Box<Fn(&Vec<RispExp>) -> Result<RispExp, RispErr>>), but then dyn fn doesn't implement Clone.
What would you recommend I do?
If you want to save on typing, make your type parameter bound into an alias.
type RispFunc<T> = Fn(&Vec<RispExp<T>>) -> Result<RispExp<T>, RispErr>;
enum RispExp<Func: RispFunc<Func>> {
...
}
Otherwise, if you use the trait object approach, you can try to implement your own clone() - How to clone a struct storing a boxed trait object? .
I have the following enum defined:
#[derive(Debug, Copy, Clone)]
struct Core;
#[derive(Debug, Copy, Clone)]
struct Mem;
#[derive(Debug, Copy, Clone)]
pub enum Atag {
Core(Core),
Mem(Mem),
Cmd(&'static str),
Unknown(u32),
None,
}
I would like to implement a function on this enum which "filters out" certain enum values. I have the following:
impl Atag {
/// Returns `Some` if this is a `Core` ATAG. Otherwise returns `None`.
pub fn core(self) -> Option<Core> {
match self {
Atag::Core => Some(self),
_ => None
}
}
}
I'm not sure why, but the compiler complains:
error[E0532]: expected unit struct/variant or constant, found tuple variant `Atag::Core`
--> src/main.rs:17:13
|
17 | Atag::Core => Some(self),
| ^^^^^^^^^^ not a unit struct/variant or constant
help: possible better candidate is found in another module, you can import it into scope
|
1 | use Core;
|
I also tried a comparison approach:
pub fn core(self) -> Option<Core> {
if self == Atag::Core {
Some(self)
} else {
None
}
}
But the compiler complains:
error[E0369]: binary operation `==` cannot be applied to type `Atag`
--> src/main.rs:20:12
|
20 | if self == Atag::Core {
| ^^^^^^^^^^^^^^^^^^
|
= note: an implementation of `std::cmp::PartialEq` might be missing for `Atag`
I think this is just a limitation of the pattern matching and is designed to prevent unexpected behavior.
The full "definition" of an Atag with type Core is Atag::Core(raw::Core). Obviously, the contents of the Core are irrelevant to you, but the compiler needs to know that everything is "accounted for" because the compiler is a stickler for the rules. The easiest way to get around this is to use the "anything pattern", _, much like you did to match non-Core variants.
impl Atag {
/// Returns `Some` if this is a `Core` ATAG. Otherwise returns `None`.
pub fn core(self) -> Option<Core> {
match self {
// The compiler now knows that a value is expected,
// but isn't necessary for the purposes of our program.
Atag::Core(_) => Some(self),
_ => None
}
}
}
To ignore multiple values, you'd use Something::Foo(_, _) - one underscore for each value in the variant, or Something::Foo(..) to ignore everything.
Remember that, unlike in some other languages, a Rust enum is not "just" a collection of different types. Data associated with an enum value is a part of it, just like the fields of a structure. So self == Atag::Core isn't a meaningful statement because it ignores the data associated with a Core. A Foo(0) is different than a Foo(12), even if they're both of the Foo variant.
I'd also like to point out if let, which is - as far as I can tell - the closest option to a standard if statement without defining a custom is_core function on Atag (which, given the existence of match and if let, is basically unnecessary).
impl Atag {
/// Returns `Some` if this is a `Core` ATAG. Otherwise returns `None`.
pub fn core(self) -> Option<Core> {
if let Atag::Core(_) = self {
Some(self)
} else {
None
}
}
}
I needed something like this to chain functions together nicely. In that case, you want to return the unwrapped core type, rather than just the enum.
I also found it easier to not consume the input, and so accepted a &self argument an returned an Option<&Core>. But you can have both.
The Rust convention has as_X as the reference-based conversion and into_X as the conversion that consumes the value. For example:
impl Atag {
fn as_core(&self) -> Option<&Core> {
if let Atag::Core(ref v) = self {
Some(v)
} else {
None
}
}
fn into_core(self) -> Option<Core> {
if let Atag::Core(v) = self {
Some(v)
} else {
None
}
}
}
fn main() {
let c = Atag::Core(Core {});
let m = Atag::Mem(Mem {});
assert_eq!(c.as_core().map(|cc| "CORE_REF"), Some("CORE_REF"));
assert_eq!(m.as_core().map(|cc| "CORE_REF"), None);
// Consume c - we cant use it after here...
assert_eq!(c.into_core().map(|cc| "NOM NOM CORE"), Some("NOM NOM CORE"));
// Consume m - we cant use it after here...
assert_eq!(m.into_core().map(|cc| "NOM NOM CORE"), None);
}
Given the following traits and implementations:
trait Wrapper<Type> {
type Inner : Wrapped<Type>;
fn bind<B, F>(self, f: F) -> <B as Wrapped<Type>>::Outer
where B: Wrapped<Type>, F: Fn(Self::Inner) -> <B as Wrapped<Type>>::Outer;
}
trait Wrapped<Type> {
type Outer : Wrapper<Type>;
}
struct Opt;
impl<T> Wrapped<Opt> for T {
type Outer = Option<T>;
}
impl<T> Wrapper<Opt> for Option<T> {
type Inner = T;
fn bind<B, F>(self, f: F) -> <B as Wrapped<Opt>>::Outer
where B: Wrapped<Opt>, F: Fn(Self::Inner) -> <B as Wrapped<Opt>>::Outer {
match self {
Some(a) => f(a),
None => None, // *** fails to compile
}
}
}
It's obvious to a human that the type <B as Wrapped<Opt>>::Outer must always be Option<B>, but rustc can't seem to figure this out:
src/main.rs:47:21: 47:25 error: mismatched types:
expected `<B as Wrapped<Opt>>::Outer`,
found `core::option::Option<_>`
(expected associated type,
found enum `core::option::Option`) [E0308]
src/main.rs:47 None => None,
^~~~
Is there any way to convince it that this is safe? I'd even settle for an unsafe solution, but mem::transmute is also not allowed since it can't prove that the types are the same size and aligned (even though there's only one real type involved and not even any newtype wrappers that could mess up alignment).
This is most likely a bug (either this one or that one, I'm not sure). Until it's fixed, I can only think of doing some custom unsafe trickery. Here's a transmute function that does the size equality checks at runtime.
unsafe fn runtime_transmute<T, U>(t: T) -> U {
assert_eq!(std::mem::size_of::<T>(), std::mem::size_of::<U>());
std::ptr::read(&t as *const _ as *const _)
}
now you can replace your None value with
unsafe { runtime_transmute(None::<T>) }
I'm starting to get comfortable with Rust, but there are still some things that are really tripping me up with lifetimes. In this particular case, what I want to do is have an enum which may have different types wrapped as a generic parameter class to create strongly typed query parameters in a URL, though the specific use case is irrelevant, and return a conversion of that wrapped value into an &str. Here's an example of what I want to do:
enum Param<'a> {
MyBool(bool),
MyLong(i64),
MyStr(&'a str),
}
impl<'a> Param<'a> {
fn into(self) -> (&'static str, &'a str) {
match self {
Param::MyBool(b) => ("my_bool", &b.to_string()), // clearly wrong
Param::MyLong(i) => ("my_long", &i.to_string()), // clearly wrong
Param::Value(s) => ("my_str", s),
}
}
}
What I ended up doing is this to deal with the obvious lifetime issue (and yes, it's obvious to me why the lifetime isn't long enough for the into() function):
enum Param<'a> {
MyBool(&'a str), // no more static typing :(
MyLong(&'a str), // no more static typing :(
MyStr(&'a str),
}
impl<'a> Param<'a> {
fn into(self) -> (&'static str, &'a str) {
match self {
Param::MyBool(b) => ("my_bool", b),
Param::MyLong(i) => ("my_long", i),
Param::Value(s) => ("my_str", s),
}
}
}
This seems like an ugly workaround in a case where what I really want to do is guarantee the static typing of certain params, b/c now it's the constructor of the enum that's responsible for the proper type conversion. Curious if there is a way to do this... and yes, at some point I need &str as that is a parameter elsewhere, specifically:
let body = url::form_urlencoded::serialize(
vec![Param::MyBool(&true.to_string()).
into()].
into_iter());
I went through a whole bunch of things like trying to return String instead of &str from into(), but that only caused conversion issues down the line with a map() of String -> &str. Having the tuple correct from the start is the easiest thing, rather than fighting the compiler at every turn after that.
-- update--
Ok, so I went back to a (String,String) tuple in the into() function for the enum. It turns out that there is an "owned" version of the url::form_urlencoded::serialize() function which this is compatible with.
pub fn serialize_owned(pairs: &[(String, String)]) -> String
But, now I'm also trying to use the same pattern for the query string in the hyper::URL, specifically:
fn set_query_from_pairs<'a, I>(&mut self, pairs: I)
where I: Iterator<Item=(&'a str, &'a str)>
and then I try to use map() on the iterator that I have from the (String,String) tuple:
params: Iterator<Item=(String, String)>
url.set_query_from_pairs(params.map(|x: (String, String)| ->
(&str, &str) { let (ref k, ref v) = x; (k, v) } ));
But this gets error: x.0 does not live long enough. Ref seems correct in this case, right? If I don't use ref, then it's k/v that don't live long enough. Is there something 'simple' that I'm missing in this?
It is not really clear why you can't do this:
enum Param<'a> {
MyBool(bool),
MyLong(i64),
MyStr(&'a str),
}
impl<'a> Param<'a> {
fn into(self) -> (&'static str, String) {
match self {
Param::MyBool(b) => ("my_bool", b.to_string()),
Param::MyLong(i) => ("my_long", i.to_string()),
Param::MyStr(s) => ("my_str", s.into()),
}
}
}
(into() for &str -> String conversion is slightly more efficient than to_string())
You can always get a &str from String, e.g. with deref coercion or explicit slicing.