This commit is contained in:
Chad Baxter 2023-06-08 22:46:29 -04:00
commit 4b6edf53fe
No known key found for this signature in database
GPG key ID: 51F5CC26A52396FC
5 changed files with 61 additions and 0 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
/target
/Cargo.lock

16
Cargo.toml Normal file
View file

@ -0,0 +1,16 @@
[package]
name = "libax"
version = "0.1.0"
edition = "2021"
authors = ["cbax <cbax@doslabelectronics.com>"]
description = "Helper functions for cbax projects"
readme = "README.md"
homepage = "https://cbax.dev/libax"
repository = "https://github.com/LogoiLab/libax"
license = "GPL-3.0-or-later"
keywords = ["helper", "auxilary"]
publish = true
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

5
README.md Normal file
View file

@ -0,0 +1,5 @@
# libax
A small collection of functions I use all the time.
Currently only implements to_u32 on Ipv4Addr and to_ipv4 on u32.

1
src/lib.rs Normal file
View file

@ -0,0 +1 @@
mod net;

37
src/net.rs Normal file
View file

@ -0,0 +1,37 @@
use std::net::Ipv4Addr;
trait ToIpv4 {
fn to_ipv4(self) -> Ipv4Addr;
}
impl ToIpv4 for u32 {
fn to_ipv4(self) -> Ipv4Addr {
return Ipv4Addr::from(self);
}
}
trait Tou32 {
fn to_u32(self) -> u32;
}
impl Tou32 for Ipv4Addr {
fn to_u32(self) -> u32 {
return self.into();
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn u32_to_ipv4() {
let ip: Ipv4Addr = 123456789.to_ipv4();
assert_eq!(ip, Ipv4Addr::new(7, 91, 205, 21));
}
#[test]
fn ipv4_to_u32() {
let num: u32 = Ipv4Addr::new(7, 91, 205, 21).to_u32();
assert_eq!(num, 123456789);
}
}