Skip to content

Commit

Permalink
Restructure public api (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
CosmicHorrorDev authored Feb 28, 2023
1 parent 882f955 commit 74a33e5
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 13 deletions.
11 changes: 4 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,11 @@ assert_eq!(third, "Welcome to earth! ");
extern crate alloc;

mod dynamic;
mod string;
mod vec;
pub mod string;
pub mod vec;

pub use self::string::*;
pub use self::vec::*;

#[cfg(test)]
mod tests;
pub use self::string::EcoString;
pub use self::vec::EcoVec;

// Run doctests on the README too
#[doc = include_str!("../README.md")]
Expand Down
18 changes: 15 additions & 3 deletions src/string.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//! A clone-on-write, small-string-optimized alternative to
//! [`String`][alloc::string::String]

use alloc::borrow::Cow;
use alloc::string::String;
use core::borrow::Borrow;
Expand All @@ -10,11 +13,11 @@ use crate::dynamic::{DynamicVec, InlineVec};

/// Create a new [`EcoString`] from a format string.
/// ```
/// # use ecow::format_eco;
/// assert_eq!(format_eco!("Hello, {}!", 123), "Hello, 123!");
/// # use ecow::eco_format;
/// assert_eq!(eco_format!("Hello, {}!", 123), "Hello, 123!");
/// ```
#[macro_export]
macro_rules! format_eco {
macro_rules! eco_format {
($($tts:tt)*) => {{
use ::std::fmt::Write;
let mut s = $crate::EcoString::new();
Expand Down Expand Up @@ -59,6 +62,15 @@ macro_rules! format_eco {
pub struct EcoString(DynamicVec);

impl EcoString {
/// Maximum number of bytes for an inline `EcoString` before spilling on
/// the heap.
///
/// The exact value for this is architecture dependent.
///
/// # Note
/// This value is semver exempt and can be changed with any update.
pub const INLINE_LIMIT: usize = crate::dynamic::LIMIT;

/// Create a new, empty string.
#[inline]
pub const fn new() -> Self {
Expand Down
2 changes: 2 additions & 0 deletions src/vec.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! A clone-on-write alternative to [`Vec`][alloc::vec::Vec]

use alloc::vec::Vec;
use core::alloc::Layout;
use core::borrow::Borrow;
Expand Down
8 changes: 5 additions & 3 deletions src/tests.rs → tests/tests.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
// Test with `cargo +nightly miri test` to check sanity!

extern crate alloc;

use alloc::boxed::Box;
use alloc::string::ToString;
use alloc::vec::Vec;
use core::mem;
use core::sync::atomic::{AtomicUsize, Ordering::*};

use super::*;
use crate::dynamic::LIMIT;
use ecow::{eco_vec, EcoString, EcoVec};

const ALPH: &str = "abcdefghijklmnopqrstuvwxyz";
const LIMIT: usize = EcoString::INLINE_LIMIT;

fn v<T>(value: T) -> Box<T> {
Box::new(value)
Expand Down Expand Up @@ -169,7 +171,7 @@ fn test_vec_truncate() {
fn test_vec_extend() {
let mut vec = EcoVec::new();
vec.extend_from_slice(&[]);
vec.extend_from_byte_slice(&[2, 3, 4]);
vec.extend_from_slice(&[2, 3, 4]);
assert_eq!(vec, [2, 3, 4]);
}

Expand Down

0 comments on commit 74a33e5

Please sign in to comment.