You can provide internals of a module via pub use

This commit is contained in:
Stephan Rehfeld 2026-05-13 18:02:48 +02:00
parent 5b4c205234
commit 41eb9a9dc0
2 changed files with 4 additions and 128 deletions

View File

@ -1,128 +1,4 @@
use std::ops::{Add, Div, Mul, Sub};
mod geometry;
mod math;
#[derive(Copy, Clone)]
pub struct Vector3 {
x: f64,
y: f64,
z: f64,
}
impl Vector3 {
pub fn new(x: f64, y: f64, z: f64) -> Vector3 {
Vector3 { x, y, z }
}
}
impl Add for Vector3 {
type Output = Vector3;
fn add(self, rhs: Vector3) -> Self::Output {
Vector3::new(self.x + rhs.x, self.y + rhs.y, self.z + rhs.z)
}
}
impl Sub for Vector3 {
type Output = Vector3;
fn sub(self, rhs: Vector3) -> Self::Output {
Vector3::new(self.x - rhs.x, self.y - rhs.y, self.z - rhs.z)
}
}
impl Mul<f64> for Vector3 {
type Output = Vector3;
fn mul(self, rhs: f64) -> Self::Output {
Vector3::new(self.x * rhs, self.y * rhs, self.z * rhs)
}
}
impl Mul<Vector3> for f64 {
type Output = Vector3;
fn mul(self, rhs: Vector3) -> Self::Output {
Vector3::new(self * rhs.x, self * rhs.y, self * rhs.z)
}
}
impl Mul for Vector3 {
type Output = f64;
fn mul(self, rhs: Vector3) -> Self::Output {
self.x * rhs.x + self.y * rhs.y + self.z * rhs.z
}
}
impl Div<f64> for Vector3 {
type Output = Vector3;
fn div(self, rhs: f64) -> Self::Output {
Vector3::new(self.x / rhs, self.y / rhs, self.z / rhs)
}
}
#[derive(Copy, Clone)]
pub struct Point3 {
x: f64,
y: f64,
z: f64,
}
impl Point3 {
pub fn new(x: f64, y: f64, z: f64) -> Point3 {
Point3 { x, y, z }
}
pub fn get_x(&self) -> f64 {
self.x
}
}
impl Add<Vector3> for Point3 {
type Output = Point3;
fn add(self, rhs: Vector3) -> Self::Output {
Point3::new(self.x + rhs.x, self.y + rhs.y, self.z + rhs.z)
}
}
impl Sub for Point3 {
type Output = Vector3;
fn sub(self, rhs: Point3) -> Self::Output {
Vector3::new(self.x - rhs.x, self.y - rhs.y, self.z - rhs.z)
}
}
pub struct Plane {
point: Point3,
normal: Vector3,
}
impl Plane {
pub fn new(point: Point3, normal: Vector3) -> Plane {
Plane { point, normal }
}
}
pub struct Line {
point: Point3,
direction: Vector3,
}
pub struct Sphere {
point: Point3,
radius: f64,
}
impl Sphere {
pub fn new(point: Point3, radius: f64) -> Sphere {
Sphere { point, radius }
}
}
pub enum Geometry {
Sphere(Sphere),
Plane(Plane),
Line(Line),
}
pub use math::Point3;

View File

@ -1,4 +1,4 @@
use hello::*;
use hello::Point3;
fn main() {
let p = Point3::new(0.0, 0.0, 0.0);