Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose WrongParameterType #111

Merged
merged 1 commit into from
Nov 9, 2024
Merged

Conversation

showier-drastic
Copy link
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
7 checks passed
@m-dahl
Copy link
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