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

Bug when using generics in a constructor function #14

Closed
bnjjj opened this issue Apr 14, 2022 · 1 comment · Fixed by #16
Closed

Bug when using generics in a constructor function #14

bnjjj opened this issue Apr 14, 2022 · 1 comment · Fixed by #16

Comments

@bnjjj
Copy link

bnjjj commented Apr 14, 2022

Example to reproduce:

#[derive(Debug)]
pub struct Request<T> {
    inner: http::Request<T>,
}

#[buildstructor::builder]
impl<T> Request<T> {
    pub fn fake_new<K, V>(
        headers: Vec<(K, V)>,
        uri: Option<http::Uri>,
        method: Option<http::Method>,
        body: T,
    ) -> http::Result<Request<T>>
    where
        HeaderName: TryFrom<K>,
        <HeaderName as TryFrom<K>>::Error: Into<http::Error>,
        HeaderValue: TryFrom<V>,
        <HeaderValue as TryFrom<V>>::Error: Into<http::Error>,
    {
        let mut builder = http::request::Builder::new().method(method).uri(uri);
        for (key, value) in headers {
            builder = builder.header(key, value);
        }
        let req = builder.body(body)?;

        Ok(Self { inner: req })
    }
}

// Use it

Request::builder()
      .header(("da".to_string(), "vda".to_string()))
      .header(("db".to_string(), "vdb".to_string()))
      .header(("db".to_string(), "vdb".to_string()))
      .header((HOST.to_string(), "host".to_string()))
      .header((CONTENT_LENGTH.to_string(), "2".to_string()))
      .header((CONTENT_TYPE.to_string(), "graphql".to_string()))
      .body(Request::builder().query("query").build())
      .build() // ERROR: Cannot find build
      .unwrap(),
@BrynCooke
Copy link
Owner

@bnjjj Thanks for this. This was pretty tricky to figure out!
Fixed in 0.1.6 release

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 a pull request may close this issue.

2 participants