声明并使用一个没有结构体的模块

huangapple go评论83阅读模式
英文:

Declaring and using a module without a struct

问题

您可以在lib.rs文件中声明模块并将其导出,然后在其他文件中引用它,而不需要在每个文件中都声明模块。这意味着您只需在lib.rs中声明img_util模块一次,然后可以在其他文件中引用它,无需在每个文件中都重复声明。

英文:

I have the following module without a struct:

img_util.rs:

  1. pub mod img_util {
  2. use image::{ImageBuffer, Pixel};
  3. use std::{fs::File, io::BufWriter};
  4. fn get_color_type<P>() -> Result<png::ColorType, std::io::Error>
  5. where
  6. P: Pixel<Subpixel = u8>,
  7. {
  8. match <P as Pixel>::CHANNEL_COUNT {
  9. 4 => Ok(png::ColorType::Rgba),
  10. 3 => Ok(png::ColorType::Rgb),
  11. _ => Err(std::io::Error::new(
  12. std::io::ErrorKind::InvalidData,
  13. "Incorrect color channel count / format.",
  14. )),
  15. }
  16. }
  17. fn get_dpi_header_data(dpi: u32) -> Vec<u8> {
  18. let dpm = 39.370079 * dpi as f32;
  19. let rounded_dpm = dpm.round() as u32;
  20. let mut data: Vec<u8> = Vec::new();
  21. data.extend_from_slice(&rounded_dpm.to_be_bytes()); // Pixels per unit in X-direction.
  22. data.extend_from_slice(&rounded_dpm.to_be_bytes()); // Pixels per unit in Y-direction.
  23. data.push(1); // Indicate that meters are used as unit.
  24. data
  25. }
  26. pub fn save_png_with_dpi<P>(
  27. input_img: &ImageBuffer<P, Vec<u8>>,
  28. output_img: File,
  29. dpi: u32,
  30. ) -> Result<(), std::io::Error>
  31. where
  32. P: Pixel<Subpixel = u8>,
  33. {
  34. let w = &mut BufWriter::new(output_img);
  35. let (width, height) = input_img.dimensions();
  36. let mut encoder = png::Encoder::new(w, width, height);
  37. encoder.set_color(get_color_type::<P>()?);
  38. encoder.set_depth(png::BitDepth::Eight);
  39. encoder.set_compression(png::Compression::Best);
  40. let data = get_dpi_header_data(dpi);
  41. let mut writer = encoder.write_header()?;
  42. writer.write_chunk(png::chunk::pHYs, data.as_slice())?;
  43. writer.write_image_data(input_img)?;
  44. Ok(())
  45. }
  46. }

lib.rs:

  1. pub mod img_util;

main.rs:

  1. use visual_center::img_util::img_util;
  2. img_util::save_png_with_dpi(
  3. // arguments
  4. ).unwrap();

I can use the module, but I feel strange about having to declare pub mod img_util twice (once in img_util.rs and another in lib.rs). Or maybe this is the only way to declare and use a module without a struct?

答案1

得分: 1

Since you have pub mod img_util; in lib.rs, the compiler will consider img_util.rs as a module of your crate.
Everything in img_util.rs is already in the img_util module.
If you state pub mod img_util { ... } in this module, this means that you provide (inline, not in another file) a submodule with the exact same name as its parent (this is a coincidence here, you could have named it differently).

You just have to remove this pub mod img_util { ... }.
Then you will use it in main.rs as

  1. use visual_center::img_util; // one less
  2. img_util::save_png_with_dpi(
  3. // arguments
  4. ).unwrap();
英文:

Since you have pub mod img_util; in lib.rs, the compiler will consider img_util.rs as a module of your crate.
Everything in img_util.rs is already in the img_util module.
If you state pub mod img_util { ... } in this module, this means that you provide (inline, not in another file) a submodule with the exact same name as its parent (this is a coincidence here, you could have named it differently).

You just have to remove this pub mod img_util { ... }.
Then you will use it in main.rs as

  1. use visual_center::img_util; // one less
  2. img_util::save_png_with_dpi(
  3. // arguments
  4. ).unwrap();

huangapple
  • 本文由 发表于 2023年5月7日 15:37:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76192695.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定