pub struct FixedText<const N: usize> { /* private fields */ }Expand description
A Text type which can store at most N bytes from a column.
The data is stored inline the type which typically means on the stack.
§Examples
use sqll::{Connection, FixedText, Result};
let c = Connection::open_in_memory()?;
c.execute(r#"
CREATE TABLE users (name TEXT);
INSERT INTO users (name) VALUES ('Alice'), ('Bob');
"#)?;
let mut stmt = c.prepare("SELECT name FROM users")?;
let ids = stmt.iter::<FixedText<10>>().collect::<Result<Vec<_>>>()?;
assert_eq!(&ids[0], "Alice");
assert_eq!(&ids[1], "Bob");Implementations§
Source§impl<const N: usize> FixedText<N>
impl<const N: usize> FixedText<N>
Sourcepub const fn from_inner(inner: FixedBlob<N>) -> Self
pub const fn from_inner(inner: FixedBlob<N>) -> Self
Converts a vector of bytes to a String without checking that the string contains valid UTF-8.
§Examples
use sqll::{FixedBlob, FixedText};
let bytes = FixedBlob::<16>::try_from(&b"Hello World"[..])?;
let s = unsafe { FixedText::from_inner(bytes) };
assert_eq!(s.as_text(), "Hello World");Sourcepub fn as_text(&self) -> &Text
pub fn as_text(&self) -> &Text
Coerce into the initialized string slice.
§Examples
use sqll::{Connection, FixedText};
let c = Connection::open_in_memory()?;
c.execute(r#"
CREATE TABLE users (name BLOB);
INSERT INTO users (name) VALUES ('Alice'), ('Bob');
"#)?;
let mut stmt = c.prepare("SELECT name FROM users")?;
assert_eq! {
stmt.iter::<FixedText<6>>().collect::<Vec<_>>(),
[Ok(FixedText::<6>::try_from("Alice")?), Ok(FixedText::<6>::try_from("Bob")?)]
};Methods from Deref<Target = Text>§
Sourcepub fn as_bytes(&self) -> &[u8] ⓘ
pub fn as_bytes(&self) -> &[u8] ⓘ
Get the underlying byte slice for this Text.
§Examples
use sqll::Text;
let t = Text::new(b"example");
assert_eq!(t.as_bytes(), b"example");Sourcepub fn to_str(&self) -> Result<&str, Utf8Error>
pub fn to_str(&self) -> Result<&str, Utf8Error>
Attempt to convert this Text into a UTF-8 string slice.
Returns an error if the underlying bytes are not valid UTF-8.
§Examples
use sqll::Text;
let t = Text::new(b"example");
assert_eq!(t.to_str()?, "example");
let invalid = Text::new(b"\xF0\x90\x80");
assert!(invalid.to_str().is_err());Trait Implementations§
Source§impl<const N: usize> AsRef<Text> for FixedText<N>
Coerce into Text.
impl<const N: usize> AsRef<Text> for FixedText<N>
Coerce into Text.
§Examples
use sqll::{FixedText, Text};
let text = FixedText::from(*b"example");
let text: &Text = text.as_ref();
assert_eq!(text, "example");Source§impl<const N: usize> Bind for FixedText<N>
impl<const N: usize> Bind for FixedText<N>
§Examples
use sqll::{Connection, FixedText};
let c = Connection::open_in_memory()?;
c.execute(r#"
CREATE TABLE users (name TEXT, age INTEGER);
INSERT INTO users (name, age) VALUES ('Alice', 42), ('Bob', 30);
"#)?;
let mut stmt = c.prepare("SELECT age FROM users WHERE name = ?")?;
stmt.bind(FixedText::<5>::try_from("Alice")?)?;
assert_eq!(stmt.iter::<i64>().collect::<Vec<_>>(), [Ok(42)]);Source§impl<const N: usize> BindValue for FixedText<N>
impl<const N: usize> BindValue for FixedText<N>
§Examples
use sqll::{Connection, FixedText, Index};
let c = Connection::open_in_memory()?;
c.execute(r#"
CREATE TABLE users (name TEXT, age INTEGER);
INSERT INTO users (name, age) VALUES ('Alice', 42), ('Bob', 30);
"#)?;
let mut stmt = c.prepare("SELECT age FROM users WHERE name = ?")?;
stmt.bind_value(Index::BIND, FixedText::<5>::try_from("Alice")?)?;
assert_eq!(stmt.iter::<i64>().collect::<Vec<_>>(), [Ok(42)]);Source§impl<const N: usize> Clone for FixedText<N>
Clone the FixedText<N>.
impl<const N: usize> Clone for FixedText<N>
Clone the FixedText<N>.
§Examples
use sqll::FixedText;
let ft1 = FixedText::<5>::try_from("Hello")?;
let ft2 = ft1.clone();
assert_eq!(ft1, ft2);Source§impl<const N: usize> Debug for FixedText<N>
Format as Text.
impl<const N: usize> Debug for FixedText<N>
Format as Text.
§Examples
use sqll::FixedText;
let ft = FixedText::<5>::try_from("Hello")?;
assert_eq!(format!("{:?}", ft), "\"Hello\"");
assert_eq!(format!("{}", ft), "Hello");Source§impl<const N: usize> Deref for FixedText<N>
Deref to Text.
impl<const N: usize> Deref for FixedText<N>
Deref to Text.
§Examples
use sqll::FixedText;
let ft = FixedText::from(*b"invalid: \xF0\x90\x80\xF0\x90\x80");
assert_eq!(ft.as_bytes(), b"invalid: \xF0\x90\x80\xF0\x90\x80");
assert_eq!(ft.to_string(), "invalid: ��");Source§impl<const N: usize> Display for FixedText<N>
The display implementation for Text will convert it into a UTF-8 string
lossily, replacing invalid sequences with the replacement character �.
impl<const N: usize> Display for FixedText<N>
The display implementation for Text will convert it into a UTF-8 string
lossily, replacing invalid sequences with the replacement character �.
§Examples
use sqll::FixedText;
let text = FixedText::from(b"before\xF0\x90\x80after");
assert_eq!(text.to_string(), "before�after");
let text = FixedText::from(b"before\xF0\x90\x80\xF0\x90\x80");
assert_eq!(text.to_string(), "before��");impl<const N: usize> Eq for FixedText<N>
Source§impl<const N: usize> From<&[u8; N]> for FixedText<N>
Attempt to convert a byte array into a FixedText<N>.
impl<const N: usize> From<&[u8; N]> for FixedText<N>
Attempt to convert a byte array into a FixedText<N>.
§Examples
use sqll::FixedText;
let ft = FixedText::from(b"Hello");
assert_eq!(ft.as_bytes(), b"Hello");
assert_eq!(ft.as_text(), "Hello");Source§impl<const N: usize> From<[u8; N]> for FixedText<N>
Attempt to convert a byte array into a FixedText<N>.
impl<const N: usize> From<[u8; N]> for FixedText<N>
Attempt to convert a byte array into a FixedText<N>.
§Examples
use sqll::FixedText;
let ft = FixedText::from(*b"Hello");
assert_eq!(ft.as_text(), "Hello");Source§impl<const N: usize> FromColumn<'_> for FixedText<N>
FromColumn implementation for FixedBlob which reads at most N
bytes.
impl<const N: usize> FromColumn<'_> for FixedText<N>
FromColumn implementation for FixedBlob which reads at most N
bytes.
If the column contains more than N bytes, a Code::MISMATCH error is
returned.
§Examples
use sqll::{Connection, FixedText, Code};
let c = Connection::open_in_memory()?;
c.execute(r#"
CREATE TABLE users (name TEXT);
INSERT INTO users (name) VALUES ('Alice'), ('Bob');
"#)?;
let mut stmt = c.prepare("SELECT name FROM users")?;
assert!(stmt.step()?.is_row());
let bytes = stmt.column::<FixedText<5>>(0)?;
assert_eq!(bytes.as_text(), "Alice");
assert!(stmt.step()?.is_row());
let e = stmt.column::<FixedText<2>>(0).unwrap_err();
assert_eq!(e.code(), Code::MISMATCH);
let bytes = stmt.column::<FixedText<5>>(0)?;
assert_eq!(bytes.as_text(), "Bob");Source§impl<const N: usize> Hash for FixedText<N>
Hash the FixedText<N>.
impl<const N: usize> Hash for FixedText<N>
Hash the FixedText<N>.
§Examples
use sqll::FixedText;
use std::collections::HashSet;
let a = FixedText::<16>::try_from("Apple")?;
let b = FixedText::<16>::try_from("Banana")?;
let mut set = HashSet::from([a, b]);
let c = FixedText::<16>::try_from("Banana")?;
assert!(set.contains(&c));
assert!(!set.insert(c));Source§impl<const N: usize> Ord for FixedText<N>
Compare for ordering.
impl<const N: usize> Ord for FixedText<N>
Compare for ordering.
§Examples
use sqll::FixedText;
use std::collections::BTreeSet;
let a = FixedText::<16>::try_from("Apple")?;
let b = FixedText::<16>::try_from("Banana")?;
let set = BTreeSet::from([a, b]);1.21.0 (const: unstable) · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl<const N: usize, const U: usize> PartialEq<FixedText<U>> for FixedText<N>
Compare the text for equality with another Text. This performs a byte-wise
comparison.
impl<const N: usize, const U: usize> PartialEq<FixedText<U>> for FixedText<N>
Compare the text for equality with another Text. This performs a byte-wise
comparison.
§Examples
use sqll::FixedText;
let t1 = FixedText::from(*b"example");
let t2 = FixedText::from(*b"example");
let t3 = FixedText::from(*b"different");
assert_eq!(t1, t2);
assert_ne!(t1, t3);Source§impl<const N: usize> PartialEq<Text> for FixedText<N>
Compare the text for equality with a Text. This performs a byte-wise
comparison.
impl<const N: usize> PartialEq<Text> for FixedText<N>
Compare the text for equality with a Text. This performs a byte-wise
comparison.
§Examples
use sqll::{FixedText, Text};
let t1 = FixedText::from(*b"example");
let t2 = Text::new("example");
let t3 = Text::new("different");
assert_eq!(t1, *t2);
assert_ne!(t1, *t3);Source§impl<const N: usize> PartialEq<str> for FixedText<N>
Compare the text for equality with a str. This performs a byte-wise
comparison.
impl<const N: usize> PartialEq<str> for FixedText<N>
Compare the text for equality with a str. This performs a byte-wise
comparison.
§Examples
use sqll::FixedText;
let t1 = FixedText::from(*b"example");
let t2 = "example";
let t3 = "different";
assert_eq!(t1, *t2);
assert_ne!(t1, *t3);Source§impl<const N: usize> PartialOrd for FixedText<N>
Compare for ordering.
impl<const N: usize> PartialOrd for FixedText<N>
Compare for ordering.
§Examples
use sqll::FixedText;
let a = FixedText::<16>::try_from("Apple")?;
let b = FixedText::<16>::try_from("Banana")?;
assert!(a < b);
assert!(b > a);Source§impl<const N: usize> TryFrom<&[u8]> for FixedText<N>
Attempt to convert a byte slice into a FixedText<N>.
impl<const N: usize> TryFrom<&[u8]> for FixedText<N>
Attempt to convert a byte slice into a FixedText<N>.
§Examples
use sqll::FixedText;
let ft = FixedText::<5>::try_from(&b"Hello"[..])?;
assert_eq!(ft.as_text(), "Hello");