Impl from traits for roblox datatypes

This commit is contained in:
Filip Tibell 2023-03-11 10:02:26 +01:00
parent 72c30d31c7
commit d23366fc98
No known key found for this signature in database
2 changed files with 36 additions and 15 deletions

View file

@ -118,10 +118,22 @@ impl DatatypeTable for Vector2 {
}
}
impl From<&RbxVector2> for Vector2 {
fn from(v: &RbxVector2) -> Self {
Vector2(Vec2 { x: v.x, y: v.y })
}
}
impl From<&Vector2> for RbxVector2 {
fn from(v: &Vector2) -> Self {
RbxVector2 { x: v.0.x, y: v.0.y }
}
}
impl FromRbxVariant for Vector2 {
fn from_rbx_variant(variant: &RbxVariant) -> RbxConversionResult<Self> {
if let RbxVariant::Vector2(v) = variant {
Ok(Vector2(Vec2 { x: v.x, y: v.y }))
Ok(v.into())
} else {
Err(RbxConversionError::FromRbxVariant {
from: variant.display_name(),
@ -138,10 +150,7 @@ impl ToRbxVariant for Vector2 {
desired_type: Option<RbxVariantType>,
) -> RbxConversionResult<RbxVariant> {
if matches!(desired_type, None | Some(RbxVariantType::Vector2)) {
Ok(RbxVariant::Vector2(RbxVector2 {
x: self.0.x,
y: self.0.y,
}))
Ok(RbxVariant::Vector2(self.into()))
} else {
Err(RbxConversionError::DesiredTypeMismatch {
can_convert_to: Some(RbxVariantType::Vector2.display_name()),

View file

@ -132,14 +132,30 @@ impl DatatypeTable for Vector3 {
}
}
impl From<&RbxVector3> for Vector3 {
fn from(v: &RbxVector3) -> Self {
Vector3(Vec3 {
x: v.x,
y: v.y,
z: v.z,
})
}
}
impl From<&Vector3> for RbxVector3 {
fn from(v: &Vector3) -> Self {
RbxVector3 {
x: v.0.x,
y: v.0.y,
z: v.0.z,
}
}
}
impl FromRbxVariant for Vector3 {
fn from_rbx_variant(variant: &RbxVariant) -> RbxConversionResult<Self> {
if let RbxVariant::Vector3(v) = variant {
Ok(Vector3(Vec3 {
x: v.x,
y: v.y,
z: v.z,
}))
Ok(v.into())
} else {
Err(RbxConversionError::FromRbxVariant {
from: variant.display_name(),
@ -156,11 +172,7 @@ impl ToRbxVariant for Vector3 {
desired_type: Option<RbxVariantType>,
) -> RbxConversionResult<RbxVariant> {
if matches!(desired_type, None | Some(RbxVariantType::Vector3)) {
Ok(RbxVariant::Vector3(RbxVector3 {
x: self.0.x,
y: self.0.y,
z: self.0.z,
}))
Ok(RbxVariant::Vector3(self.into()))
} else {
Err(RbxConversionError::DesiredTypeMismatch {
can_convert_to: Some(RbxVariantType::Vector3.display_name()),