Skip to content

Commit

Permalink
Add missing trait impls for VString (#89)
Browse files Browse the repository at this point in the history
  • Loading branch information
maciejhirsz committed May 26, 2023
1 parent 3178c41 commit 6ffab77
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/kobold/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "kobold"
version = "0.9.0"
version = "0.9.1"
authors = ["Maciej Hirsz <hello@maciej.codes>"]
edition = "2021"
license = "MPL-2.0"
Expand Down
54 changes: 53 additions & 1 deletion crates/kobold/src/diff/vstring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

use crate::diff::Diff;
use std::fmt::{self, Debug, Display, Write};
use std::hash::{Hash, Hasher};
use std::ops::{Deref, DerefMut};

use crate::diff::Diff;

/// Versioned string.
///
/// This type acts as a drop-in replacement for a regular owned [`String`](String). Internally its
Expand All @@ -20,6 +23,7 @@ use std::ops::{Deref, DerefMut};
///
/// You may add the `serde` feature to the `kobold` crate to add support for the `serde::Serialize` and
/// `serde::Deserialize` traits.
#[derive(Default)]
pub struct VString {
inner: String,
ver: usize,
Expand Down Expand Up @@ -110,6 +114,45 @@ where
}
}

impl PartialEq<VString> for VString {
fn eq(&self, other: &VString) -> bool {
self.inner.eq(&other.inner)
}
}

impl Eq for VString {}

impl PartialOrd<VString> for VString {
fn partial_cmp(&self, other: &VString) -> Option<std::cmp::Ordering> {
self.inner.partial_cmp(&other.inner)
}
}

impl Ord for VString {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.inner.cmp(&other.inner)
}
}

impl Debug for VString {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Debug::fmt(&self.inner, f)
}
}

impl Display for VString {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Display::fmt(&self.inner, f)
}
}

impl Write for VString {
fn write_str(&mut self, s: &str) -> fmt::Result {
self.ver += 1;
self.inner.write_str(s)
}
}

impl<A> FromIterator<A> for VString
where
String: FromIterator<A>,
Expand All @@ -125,6 +168,15 @@ where
}
}

impl Hash for VString {
fn hash<H>(&self, state: &mut H)
where
H: Hasher,
{
self.inner.hash(state)
}
}

#[cfg(feature = "serde")]
mod serde {
use serde::de::{Deserialize, Deserializer};
Expand Down

0 comments on commit 6ffab77

Please sign in to comment.