Skip to content

Expose WrongParameterType#111

Merged
m-dahl merged 1 commit intosequenceplanner:masterfrom
showier-drastic:master
Nov 9, 2024
Merged

Expose WrongParameterType#111
m-dahl merged 1 commit intosequenceplanner:masterfrom
showier-drastic:master

Conversation

@showier-drastic
Copy link
Copy Markdown
Contributor

I'm creating a compatibility layer of multiple robotics libraries, and I found that I need to write code like this:

pub struct NodeHandler<'b>(&'b mut r2r::Node);

pub trait ParamType: Sized
where
    ParameterValue: TryInto<Self, Error = WrongParameterType>,
{
    fn get(node: &mut NodeHandler, key: &str) -> Result<Self> {
        let result = node.0.get_parameter::<Self>(key);
        match result {
            Ok(val) => Ok(val),
            Err(r2r::Error::ParameterWrongType {
                name: _,
                expected_type: _,
                actual_type: "not set",
            }) => Err(crate::Error::ParameterNotFound(key.to_owned())),
            Err(e) => Err(e.into()),
        }
    }
}

impl ParamType for bool {}
impl ParamType for String {}
impl ParamType for i64 {}
impl ParamType for f64 {}

impl ParamType for Vec<bool> {}
impl ParamType for Vec<String> {}
impl ParamType for Vec<i64> {}
impl ParamType for Vec<f64> {}

In order to call get_parameter, I need to add a trait bound ParameterValue: TryInto<Self, Error = WrongParameterType> (the same generic bound for Node::get_parameter). So, it is needed to have WrongParameterType.

@m-dahl m-dahl merged commit 4517663 into sequenceplanner:master Nov 9, 2024
@m-dahl
Copy link
Copy Markdown
Collaborator

m-dahl commented Nov 9, 2024

Makes sense, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants