2021-02-17 12:40:27 +00:00
|
|
|
use crate::{CID, Error, MemoryMessage, Result};
|
2021-02-14 16:05:42 +00:00
|
|
|
|
|
|
|
|
use rkyv::Write;
|
|
|
|
|
use rkyv::Unarchive;
|
|
|
|
|
use rkyv::archived_value;
|
|
|
|
|
use core::pin::Pin;
|
|
|
|
|
|
2021-02-17 18:01:11 +00:00
|
|
|
#[derive(Copy, Clone)]
|
2021-02-14 16:05:42 +00:00
|
|
|
pub struct String<const N: usize> {
|
|
|
|
|
bytes: [u8; N],
|
2021-01-21 17:33:06 +00:00
|
|
|
len: u32,
|
2020-10-30 03:18:48 +00:00
|
|
|
}
|
|
|
|
|
|
2021-02-14 16:05:42 +00:00
|
|
|
impl<const N: usize> String<N> {
|
|
|
|
|
pub fn new() -> String<N> {
|
2020-10-30 03:18:48 +00:00
|
|
|
String {
|
2021-02-14 16:05:42 +00:00
|
|
|
bytes: [0; N],
|
2020-10-30 03:18:48 +00:00
|
|
|
len: 0,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-17 15:29:20 +00:00
|
|
|
pub fn as_bytes(&self) -> [u8; N] {
|
|
|
|
|
self.bytes
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-14 16:05:42 +00:00
|
|
|
pub fn as_str(&self) -> core::result::Result<&str, core::str::Utf8Error> {
|
|
|
|
|
core::str::from_utf8(&self.bytes[0..self.len as usize])
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-30 12:48:34 +00:00
|
|
|
pub fn len(&self) -> usize {
|
2021-01-21 17:33:06 +00:00
|
|
|
self.len as usize
|
2020-10-30 12:48:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn is_empty(&self) -> bool {
|
|
|
|
|
self.len == 0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Convert a `MemoryMessage` into a `String`
|
2021-01-03 10:31:51 +00:00
|
|
|
pub fn from_message(
|
2021-02-14 16:05:42 +00:00
|
|
|
message: & mut MemoryMessage,
|
|
|
|
|
) -> core::result::Result<String<N>, core::str::Utf8Error> {
|
2021-02-17 12:40:27 +00:00
|
|
|
let buf = unsafe{ crate::XousBuffer::from_memory_message(message) };
|
2021-02-14 16:05:42 +00:00
|
|
|
let bytes = Pin::new(buf.as_ref());
|
|
|
|
|
let value = unsafe {
|
|
|
|
|
archived_value::<String<N>>(&bytes, message.id as usize)
|
|
|
|
|
};
|
|
|
|
|
let s = value.unarchive();
|
|
|
|
|
Ok(s)
|
2020-10-30 12:48:34 +00:00
|
|
|
}
|
|
|
|
|
|
2020-10-30 03:18:48 +00:00
|
|
|
/// Perform an immutable lend of this String to the specified server.
|
|
|
|
|
/// This function will block until the server returns.
|
2021-02-17 15:29:20 +00:00
|
|
|
/// Note that this convenience should only be used if the server only ever
|
|
|
|
|
/// expects to deal with one type of String, ever. Otherwise, this should be
|
|
|
|
|
/// implemented in the API and wrapped in an Enum to help decorate the functional
|
|
|
|
|
/// target of the string. An example of a server that uses this convencience function
|
|
|
|
|
/// is the logger.
|
2021-01-03 10:31:51 +00:00
|
|
|
pub fn lend(
|
|
|
|
|
&self,
|
|
|
|
|
connection: CID,
|
2021-02-17 12:54:08 +00:00
|
|
|
// id: crate::MessageId,
|
2021-01-03 10:31:51 +00:00
|
|
|
) -> core::result::Result<Result, Error> {
|
2021-02-14 16:05:42 +00:00
|
|
|
|
2021-02-14 20:42:30 +00:00
|
|
|
let mut writer = rkyv::ArchiveBuffer::new(crate::XousBuffer::new( N ));
|
2021-02-14 16:05:42 +00:00
|
|
|
let pos = writer.archive(self).expect("xous::String -- couldn't archive self");
|
2021-02-17 12:54:08 +00:00
|
|
|
let xous_buffer = writer.into_inner();
|
2021-02-14 16:05:42 +00:00
|
|
|
|
2021-02-14 20:42:30 +00:00
|
|
|
// note that "id" is actually used as the position into the rkyv buffer
|
2021-02-14 16:05:42 +00:00
|
|
|
xous_buffer.lend(connection, pos as u32)
|
2020-10-30 03:18:48 +00:00
|
|
|
}
|
|
|
|
|
|
2020-11-01 08:28:47 +00:00
|
|
|
/// Move this string from the client into the server.
|
2021-01-03 10:31:51 +00:00
|
|
|
pub fn send(
|
|
|
|
|
self,
|
|
|
|
|
connection: CID,
|
2021-02-17 15:29:20 +00:00
|
|
|
// id: crate::MessageId,
|
2021-01-03 10:31:51 +00:00
|
|
|
) -> core::result::Result<Result, Error> {
|
2021-02-14 20:42:30 +00:00
|
|
|
let mut writer = rkyv::ArchiveBuffer::new(crate::XousBuffer::new(/*self.bytes.len()*/ 4096));
|
|
|
|
|
let pos = writer.archive(&self).expect("xous::String -- couldn't archive self");
|
2021-02-17 15:29:20 +00:00
|
|
|
let xous_buffer = writer.into_inner();
|
2021-02-14 20:42:30 +00:00
|
|
|
|
|
|
|
|
xous_buffer.send(connection, pos as u32)
|
2020-11-01 08:28:47 +00:00
|
|
|
}
|
2021-02-17 15:29:20 +00:00
|
|
|
|
2020-10-30 03:18:48 +00:00
|
|
|
/// Clear the contents of this String and set the length to 0
|
|
|
|
|
pub fn clear(&mut self) {
|
|
|
|
|
self.len = 0;
|
2021-02-14 16:05:42 +00:00
|
|
|
self.bytes = [0; N];
|
2020-10-30 03:18:48 +00:00
|
|
|
}
|
2021-01-19 10:58:57 +00:00
|
|
|
|
|
|
|
|
pub fn to_str(&self) -> &str {
|
2021-02-14 16:05:42 +00:00
|
|
|
unsafe { core::str::from_utf8_unchecked(&self.bytes[0..self.len()]) }
|
2021-01-19 10:58:57 +00:00
|
|
|
}
|
2020-10-30 03:18:48 +00:00
|
|
|
}
|
|
|
|
|
|
2021-02-14 16:05:42 +00:00
|
|
|
impl<const N: usize> core::fmt::Display for String<N> {
|
2020-10-30 03:18:48 +00:00
|
|
|
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
2021-02-14 16:05:42 +00:00
|
|
|
write!(f, "{}", self.to_str())
|
2020-10-30 03:18:48 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-14 16:05:42 +00:00
|
|
|
impl<const N: usize> core::fmt::Write for String<N> {
|
2020-10-30 03:18:48 +00:00
|
|
|
fn write_str(&mut self, s: &str) -> core::result::Result<(), core::fmt::Error> {
|
|
|
|
|
for c in s.bytes() {
|
2021-02-14 16:05:42 +00:00
|
|
|
if self.len() < self.bytes.len() {
|
|
|
|
|
self.bytes[self.len()] = c;
|
2020-10-30 03:18:48 +00:00
|
|
|
self.len += 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-01-17 09:00:57 +00:00
|
|
|
|
2021-02-14 16:05:42 +00:00
|
|
|
impl<const N: usize> core::fmt::Debug for String<N> {
|
2021-01-17 09:00:57 +00:00
|
|
|
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
2021-02-14 16:05:42 +00:00
|
|
|
write!(f, "{}", self.to_str())
|
2021-01-17 09:00:57 +00:00
|
|
|
}
|
|
|
|
|
}
|
2021-02-14 16:05:42 +00:00
|
|
|
|
|
|
|
|
pub struct ArchivedString {
|
|
|
|
|
ptr: rkyv::RelPtr,
|
|
|
|
|
len: u32,
|
|
|
|
|
}
|
|
|
|
|
pub struct StringResolver {
|
|
|
|
|
bytes_pos: usize,
|
|
|
|
|
}
|
|
|
|
|
impl<const N: usize> rkyv::Resolve<String<N>> for StringResolver {
|
|
|
|
|
type Archived = ArchivedString;
|
|
|
|
|
|
|
|
|
|
fn resolve(self, pos: usize, value: &String<N>) -> Self::Archived {
|
|
|
|
|
Self::Archived {
|
|
|
|
|
ptr: unsafe {
|
|
|
|
|
rkyv::RelPtr::new(
|
|
|
|
|
pos + rkyv::offset_of!(ArchivedString, ptr),
|
|
|
|
|
self.bytes_pos)
|
|
|
|
|
},
|
|
|
|
|
len: value.len() as u32,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<const N: usize> rkyv::Archive for String<N> {
|
|
|
|
|
type Archived = ArchivedString;
|
|
|
|
|
type Resolver = StringResolver;
|
|
|
|
|
|
|
|
|
|
fn archive<W: rkyv::Write + ?Sized>(&self, writer: &mut W) -> core::result::Result<Self::Resolver, W::Error> {
|
|
|
|
|
let bytes_pos = writer.pos();
|
|
|
|
|
writer.write(&self.bytes[0..self.len()])?;
|
|
|
|
|
Ok(Self::Resolver { bytes_pos })
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
impl<const N: usize> rkyv::Unarchive<String<N>> for ArchivedString {
|
|
|
|
|
fn unarchive(&self) -> String<N> {
|
|
|
|
|
let mut s: String<N> = String::<N>::new();
|
|
|
|
|
unsafe {
|
|
|
|
|
let p = self.ptr.as_ptr() as *const u8;
|
|
|
|
|
for(i, val) in s.bytes.iter_mut().enumerate() {
|
|
|
|
|
*val = p.add(i).read();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
s.len = self.len;
|
|
|
|
|
s
|
|
|
|
|
}
|
2021-02-17 12:40:27 +00:00
|
|
|
}
|