If a public function returns a type that's defined in a private module, how do I store the resulting value in a struct? If I try to define the struct using the type, I get an error that the type is private.
If all you need to know about the type is that it implements some trait, then you could convert the value into a Box<dyn SomeTrait>
and store that. This is often used for futures in async Rust.
Besides this, there's no way to do it.
Hmm. Is that generally a bad practice? Should I ask the library author to make the type public? It seems strange to me that I can get the value, but can't keep it.
You might be not supposed to store this value in struct, only in local variable, where the type can be inferred. However, I agree that this looks like a very edge case.
Can you link to the function? Sometimes there are good reasons.
(it won't let me post more than two links, please excuse the plain text)
PR https://github.com/sfackler/rust-postgres/pull/778
Function defined here:
Type defined here:
https://github.com/sfackler/rust-postgres/blob/909f2dce67159d91577134773f6739d2a03ff1e6/tokio-postgres/src/copy_both.rs#L229
Module declared here:
https://github.com/sfackler/rust-postgres/blob/909f2dce67159d91577134773f6739d2a03ff1e6/tokio-postgres/src/lib.rs#L160
I left a comment on the PR that it should include pub use crate::copy_both::CopyBothDuplex
which seems to match the similar CopyOutStream
and CopyInSink
.
This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.