Tommi (cc'ing rust-dev)-
I don't know if this is considered an anti-pattern or not, but if you want to
structure your files in the manner you describe, you can do so without
resorting to `#[path=…]`, assuming you're willing to add a bit of boilerplate
to the foo.rs and bar.rs to pull in the modules that are in the subdirectories.
I've included a transcript describing a tiny example of your layout
("variant1") versus the mod.rs-based layout ("variant2"). The main difference
is that one needs to put declarations of the modules in the subdirectory into a
nested private mod (`mod foo { pub mod lut; }`) and a reexport (`pub use lut =
self::foo::lut`) within "foo.rs" in the parent directory.
(In case its not obvious from the context, it is legal to put the `foo` mod at
either <dir>/foo.rs or at <dir>/foo/mod.rs; the compiler will look in both
places for it. If you put a file for the `foo` mod in both places, the
compiler signals an error since that is an ambiguity.)
Cheers,
-Felix
Transcript illustrating directory layout and how to make your code accommodate
either layout.
% find variant1 -type file
variant1/foo/lut.rs
variant1/foo.rs
variant1/main.rs
% find variant2 -type file
variant2/foo/lut.rs
variant2/foo/mod.rs
variant2/main.rs
% rustc variant1/main.rs && ./main
m/variant1/foo.rs m/variant1/foo/lut.rs
% rustc variant2/main.rs && ./main
m/variant2/foo/mod.rs m/variant2/foo/lut.rs
% find variant1 -type file -exec echo == {} == \; -exec cat {} \;
== variant1/foo/lut.rs ==
pub fn lut() -> ~str { ~"m/variant1/foo/lut.rs" }
== variant1/foo.rs ==
pub use lut = self::foo::lut;
pub fn foo() -> ~str { ~"m/variant1/foo.rs" }
mod foo {
pub mod lut;
}
== variant1/main.rs ==
mod foo;
fn main() {
println!("{} {}", foo::foo(), foo::lut::lut());
}
% find variant2 -type file -exec echo == {} == \; -exec cat {} \;
== variant2/foo/lut.rs ==
pub fn lut() -> ~str { ~"m/variant2/foo/lut.rs" }
== variant2/foo/mod.rs ==
pub fn foo() -> ~str { ~"m/variant2/foo/mod.rs" }
pub mod lut;
== variant2/main.rs ==
mod foo;
fn main() {
println!("{} {}", foo::foo(), foo::lut::lut());
}
%
On 17 Apr 2014, at 16:39, Tommi <[email protected]> wrote:
> Can someone explain me why the module system maps to the file system in the
> way it does? The problem is that you can end up with these modules named
> mod.rs instead of the more informative names. If you have the following
> modules:
>
> foo
> foo::lut
> bar
> bar::lut
>
> ...that maps to files and folders as such:
>
> foo/mod.rs
> foo/lut.rs
> bar/mod.rs
> bar/lut.rs
>
> ...but why not map such modules to files and folders as the following:
>
> foo.rs
> foo/lut.rs
> bar.rs
> bar/lut.rs
>
> ...and have each module informatively named.
>
> _______________________________________________
> Rust-dev mailing list
> [email protected]
> https://mail.mozilla.org/listinfo/rust-dev
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev