feat: implement TryFrom<Object> for NvimString

This commit is contained in:
Riccardo Mazzarini 2025-05-23 15:14:18 +02:00
parent 75e4b36478
commit e9bb99794a
No known key found for this signature in database
GPG key ID: 38165222613796F5
2 changed files with 16 additions and 15 deletions

View file

@ -113,19 +113,6 @@ impl FromObject for Float {
}
}
impl FromObject for crate::String {
fn from_object(obj: Object) -> Result<Self, Error> {
match obj.kind() {
ObjectKind::String => Ok(unsafe { obj.into_string_unchecked() }),
other => Err(Error::FromWrongType {
expected: "string",
actual: other.as_static(),
}),
}
}
}
impl FromObject for Array {
fn from_object(obj: Object) -> Result<Self, Error> {
match obj.kind() {

View file

@ -8,8 +8,7 @@ use std::path::{Path, PathBuf};
use luajit as lua;
use crate::NvimStr;
use crate::StringBuilder;
use crate::{conversion, NvimStr, Object, ObjectKind, StringBuilder};
/// Binding to the string type used by Neovim.
///
@ -234,6 +233,21 @@ impl PartialEq<std::string::String> for String {
}
}
impl TryFrom<Object> for String {
type Error = conversion::Error;
#[inline]
fn try_from(obj: crate::Object) -> Result<Self, Self::Error> {
match obj.kind() {
ObjectKind::String => Ok(unsafe { obj.into_string_unchecked() }),
other => Err(conversion::Error::FromWrongType {
expected: "string",
actual: other.as_static(),
}),
}
}
}
impl lua::Pushable for String {
#[inline]
unsafe fn push(