livesplit-core 0.13.0

livesplit-core is a library that provides a lot of functionality for creating a speedrun timer.
Documentation
use crate::platform::prelude::*;

/// A type that can be used to populate an owned [`String`]. This is useful for
/// reducing unnecessary memory allocations.
pub trait PopulateString: Sized {
    /// Populate the [`String`] with the contents of this type.
    fn populate(self, buf: &mut String);

    /// Accesses the contents of this type as a [`str`].
    fn as_str(&self) -> &str;

    /// Turns this type into a [`String`].
    fn into_string(self) -> String {
        let mut buf = String::new();
        self.populate(&mut buf);
        buf
    }
}

impl PopulateString for String {
    fn populate(self, buf: &mut String) {
        *buf = self;
    }
    fn as_str(&self) -> &str {
        self
    }
}

impl PopulateString for &str {
    fn populate(self, buf: &mut String) {
        if self.len() <= buf.capacity() {
            buf.clear();
            buf.push_str(self);
        } else {
            *buf = self.to_owned();
        }
    }
    fn as_str(&self) -> &str {
        self
    }
}

impl PopulateString for alloc::borrow::Cow<'_, str> {
    fn populate(self, buf: &mut String) {
        match self {
            alloc::borrow::Cow::Borrowed(s) => s.populate(buf),
            alloc::borrow::Cow::Owned(s) => s.populate(buf),
        }
    }
    fn as_str(&self) -> &str {
        self
    }
}