#![allow(dead_code)]
use core::fmt;
use flexstr::{FlexStr, RefCounted, StringLike};
use flexstr_support::StringToFromBytes;
use inline_flexstr::InlineFlexStr;
pub fn test_to_owned<S, R>(s: &'static S)
where
S: ?Sized + StringToFromBytes + fmt::Debug + PartialEq,
R: RefCounted<S>,
Box<S>: From<S::Owned>,
S::Owned: AsRef<S>,
{
let borrowed: FlexStr<'_, S, R> = FlexStr::from_borrowed(s);
let owned = borrowed.to_owned();
assert_eq!(owned.as_ref_type(), s);
assert!(matches!(
owned,
FlexStr::Inlined(_) | FlexStr::RefCounted(_)
));
}
pub fn test_into_owned<S, R>(s: &'static S)
where
S: ?Sized + StringToFromBytes + fmt::Debug + PartialEq,
R: RefCounted<S>,
Box<S>: From<S::Owned>,
S::Owned: AsRef<S>,
{
let borrowed: FlexStr<'_, S, R> = FlexStr::from_borrowed(s);
let owned = borrowed.into_owned();
assert_eq!(owned.as_ref_type(), s);
assert!(!owned.is_borrowed());
}
pub fn test_to_owned_type<S, R>(s: &'static S)
where
S: ?Sized + StringToFromBytes + fmt::Debug + PartialEq,
R: RefCounted<S>,
S::Owned: PartialEq + AsRef<S>,
FlexStr<'static, S, R>: StringLike<S>,
{
let flex_str: FlexStr<'_, S, R> = FlexStr::from_borrowed(s);
let owned = StringLike::to_owned_type(&flex_str);
assert_eq!(owned.as_ref(), s);
}
pub fn test_into_owned_type<S, R>(s: &'static S)
where
S: ?Sized + StringToFromBytes + PartialEq + fmt::Debug,
R: RefCounted<S>,
Box<S>: From<S::Owned>,
S::Owned: PartialEq + AsRef<S> + From<Box<S>>,
FlexStr<'static, S, R>: StringLike<S>,
{
let flex_str: FlexStr<'_, S, R> = FlexStr::from_borrowed(s);
let owned = StringLike::into_owned_type(flex_str);
assert_eq!(owned.as_ref(), s);
}
pub fn test_to_local<S>(s: &'static S)
where
S: ?Sized + StringToFromBytes + fmt::Debug + PartialEq,
alloc::sync::Arc<S>: for<'a> From<&'a S>,
alloc::rc::Rc<S>: for<'a> From<&'a S>,
{
use alloc::sync::Arc;
let shared: FlexStr<'_, S, Arc<S>> = FlexStr::from_borrowed(s);
let local = shared.to_local();
assert_eq!(local.as_ref_type(), s);
assert!(local.is_borrowed() || local.is_inlined() || local.is_ref_counted());
}
pub fn test_into_local<S>(s: &'static S)
where
S: ?Sized + StringToFromBytes + fmt::Debug + PartialEq,
alloc::sync::Arc<S>: for<'a> From<&'a S>,
alloc::rc::Rc<S>: for<'a> From<&'a S>,
{
use alloc::sync::Arc;
let shared: FlexStr<'_, S, Arc<S>> = FlexStr::from_borrowed(s);
let local = shared.into_local();
assert_eq!(local.as_ref_type(), s);
}
pub fn test_to_shared<S>(s: &'static S)
where
S: ?Sized + StringToFromBytes + fmt::Debug + PartialEq,
alloc::sync::Arc<S>: for<'a> From<&'a S>,
alloc::rc::Rc<S>: for<'a> From<&'a S>,
{
use alloc::rc::Rc;
let local: FlexStr<'_, S, Rc<S>> = FlexStr::from_borrowed(s);
let shared = local.to_shared();
assert_eq!(shared.as_ref_type(), s);
assert!(shared.is_borrowed() || shared.is_inlined() || shared.is_ref_counted());
}
pub fn test_into_shared<S>(s: &'static S)
where
S: ?Sized + StringToFromBytes + fmt::Debug + PartialEq,
alloc::sync::Arc<S>: for<'a> From<&'a S>,
alloc::rc::Rc<S>: for<'a> From<&'a S>,
{
use alloc::rc::Rc;
let local: FlexStr<'_, S, Rc<S>> = FlexStr::from_borrowed(s);
let shared = local.into_shared();
assert_eq!(shared.as_ref_type(), s);
}
pub fn test_optimize<S, R>(s: &'static S)
where
S: ?Sized + StringToFromBytes + fmt::Debug + PartialEq,
R: RefCounted<S>,
Box<S>: From<S::Owned>,
S::Owned: AsRef<S>,
{
let boxed: FlexStr<'_, S, R> = FlexStr::from_boxed(Box::from(s.to_owned()));
let optimized = boxed.optimize();
assert_eq!(optimized.as_ref_type(), s);
assert!(matches!(
optimized,
FlexStr::Inlined(_) | FlexStr::RefCounted(_)
));
let borrowed: FlexStr<'_, S, R> = FlexStr::from_borrowed(s);
let optimized = borrowed.optimize();
assert_eq!(optimized.as_ref_type(), s);
assert!(optimized.is_borrowed());
}
pub fn test_from_borrowed_ref<S, R>(s: &'static S)
where
S: ?Sized + StringToFromBytes + fmt::Debug + PartialEq,
R: RefCounted<S>,
{
let flex_str: FlexStr<'_, S, R> = s.into();
assert!(flex_str.is_borrowed());
assert_eq!(flex_str.as_ref_type(), s);
}
pub fn test_from_box<S, R>(s: &'static S)
where
S: ?Sized + StringToFromBytes + fmt::Debug + PartialEq,
R: RefCounted<S>,
Box<S>: From<S::Owned>,
S::Owned: AsRef<S>,
{
let boxed = Box::from(s.to_owned());
let flex_str: FlexStr<'_, S, R> = boxed.into();
assert!(flex_str.is_boxed());
assert_eq!(flex_str.as_ref_type(), s);
}
pub fn test_from_inline_flex_str<S, R>(s: &'static S)
where
S: ?Sized + StringToFromBytes + fmt::Debug + PartialEq,
R: RefCounted<S>,
{
let inline_str =
InlineFlexStr::try_from_type(s).expect("test input should be small enough to inline");
let flex_str: FlexStr<'_, S, R> = inline_str.into();
assert!(flex_str.is_inlined());
assert_eq!(flex_str.as_ref_type(), s);
}
pub fn test_from_cow<S, R>(s: &'static S)
where
S: ?Sized + StringToFromBytes + fmt::Debug + PartialEq,
R: RefCounted<S>,
Box<S>: From<S::Owned>,
S::Owned: AsRef<S>,
{
use alloc::borrow::Cow;
let cow: Cow<'_, S> = Cow::Borrowed(s);
let flex_str: FlexStr<'_, S, R> = cow.into();
assert!(flex_str.is_borrowed());
assert_eq!(flex_str.as_ref_type(), s);
let cow: Cow<'_, S> = Cow::Owned(s.to_owned());
let flex_str: FlexStr<'_, S, R> = cow.into();
assert!(flex_str.is_boxed());
assert_eq!(flex_str.as_ref_type(), s);
}