Skip to content

Commit 4b9d1cf

Browse files
committed
Replace impl ToString with Into<StdString>
This is a more canonical way to accept any types of stirng but not arbitrary types that implement `Display`
1 parent 55c07f3 commit 4b9d1cf

File tree

2 files changed

+70
-63
lines changed

2 files changed

+70
-63
lines changed

src/userdata.rs

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,13 @@ impl AsRef<str> for MetaMethod {
240240
}
241241
}
242242

243+
impl From<MetaMethod> for StdString {
244+
#[inline]
245+
fn from(method: MetaMethod) -> Self {
246+
method.name().to_owned()
247+
}
248+
}
249+
243250
/// Method registry for [`UserData`] implementors.
244251
pub trait UserDataMethods<T> {
245252
/// Add a regular method which accepts a `&T` as the first parameter.
@@ -249,7 +256,7 @@ pub trait UserDataMethods<T> {
249256
///
250257
/// If `add_meta_method` is used to set the `__index` metamethod, the `__index` metamethod will
251258
/// be used as a fall-back if no regular method is found.
252-
fn add_method<M, A, R>(&mut self, name: impl ToString, method: M)
259+
fn add_method<M, A, R>(&mut self, name: impl Into<StdString>, method: M)
253260
where
254261
M: Fn(&Lua, &T, A) -> Result<R> + MaybeSend + 'static,
255262
A: FromLuaMulti,
@@ -260,7 +267,7 @@ pub trait UserDataMethods<T> {
260267
/// Refer to [`add_method`] for more information about the implementation.
261268
///
262269
/// [`add_method`]: UserDataMethods::add_method
263-
fn add_method_mut<M, A, R>(&mut self, name: impl ToString, method: M)
270+
fn add_method_mut<M, A, R>(&mut self, name: impl Into<StdString>, method: M)
264271
where
265272
M: FnMut(&Lua, &mut T, A) -> Result<R> + MaybeSend + 'static,
266273
A: FromLuaMulti,
@@ -273,7 +280,7 @@ pub trait UserDataMethods<T> {
273280
/// [`add_method`]: UserDataMethods::add_method
274281
#[cfg(feature = "async")]
275282
#[cfg_attr(docsrs, doc(cfg(feature = "async")))]
276-
fn add_async_method<M, A, MR, R>(&mut self, name: impl ToString, method: M)
283+
fn add_async_method<M, A, MR, R>(&mut self, name: impl Into<StdString>, method: M)
277284
where
278285
T: 'static,
279286
M: Fn(Lua, UserDataRef<T>, A) -> MR + MaybeSend + 'static,
@@ -288,7 +295,7 @@ pub trait UserDataMethods<T> {
288295
/// [`add_method`]: UserDataMethods::add_method
289296
#[cfg(feature = "async")]
290297
#[cfg_attr(docsrs, doc(cfg(feature = "async")))]
291-
fn add_async_method_mut<M, A, MR, R>(&mut self, name: impl ToString, method: M)
298+
fn add_async_method_mut<M, A, MR, R>(&mut self, name: impl Into<StdString>, method: M)
292299
where
293300
T: 'static,
294301
M: Fn(Lua, UserDataRefMut<T>, A) -> MR + MaybeSend + 'static,
@@ -301,7 +308,7 @@ pub trait UserDataMethods<T> {
301308
/// The first argument will be a [`AnyUserData`] of type `T` if the method is called with Lua
302309
/// method syntax: `my_userdata:my_method(arg1, arg2)`, or it is passed in as the first
303310
/// argument: `my_userdata.my_method(my_userdata, arg1, arg2)`.
304-
fn add_function<F, A, R>(&mut self, name: impl ToString, function: F)
311+
fn add_function<F, A, R>(&mut self, name: impl Into<StdString>, function: F)
305312
where
306313
F: Fn(&Lua, A) -> Result<R> + MaybeSend + 'static,
307314
A: FromLuaMulti,
@@ -312,7 +319,7 @@ pub trait UserDataMethods<T> {
312319
/// This is a version of [`add_function`] that accepts a `FnMut` argument.
313320
///
314321
/// [`add_function`]: UserDataMethods::add_function
315-
fn add_function_mut<F, A, R>(&mut self, name: impl ToString, function: F)
322+
fn add_function_mut<F, A, R>(&mut self, name: impl Into<StdString>, function: F)
316323
where
317324
F: FnMut(&Lua, A) -> Result<R> + MaybeSend + 'static,
318325
A: FromLuaMulti,
@@ -326,7 +333,7 @@ pub trait UserDataMethods<T> {
326333
/// [`add_function`]: UserDataMethods::add_function
327334
#[cfg(feature = "async")]
328335
#[cfg_attr(docsrs, doc(cfg(feature = "async")))]
329-
fn add_async_function<F, A, FR, R>(&mut self, name: impl ToString, function: F)
336+
fn add_async_function<F, A, FR, R>(&mut self, name: impl Into<StdString>, function: F)
330337
where
331338
F: Fn(Lua, A) -> FR + MaybeSend + 'static,
332339
A: FromLuaMulti,
@@ -341,7 +348,7 @@ pub trait UserDataMethods<T> {
341348
/// side has a metatable. To prevent this, use [`add_meta_function`].
342349
///
343350
/// [`add_meta_function`]: UserDataMethods::add_meta_function
344-
fn add_meta_method<M, A, R>(&mut self, name: impl ToString, method: M)
351+
fn add_meta_method<M, A, R>(&mut self, name: impl Into<StdString>, method: M)
345352
where
346353
M: Fn(&Lua, &T, A) -> Result<R> + MaybeSend + 'static,
347354
A: FromLuaMulti,
@@ -355,7 +362,7 @@ pub trait UserDataMethods<T> {
355362
/// side has a metatable. To prevent this, use [`add_meta_function`].
356363
///
357364
/// [`add_meta_function`]: UserDataMethods::add_meta_function
358-
fn add_meta_method_mut<M, A, R>(&mut self, name: impl ToString, method: M)
365+
fn add_meta_method_mut<M, A, R>(&mut self, name: impl Into<StdString>, method: M)
359366
where
360367
M: FnMut(&Lua, &mut T, A) -> Result<R> + MaybeSend + 'static,
361368
A: FromLuaMulti,
@@ -371,7 +378,7 @@ pub trait UserDataMethods<T> {
371378
docsrs,
372379
doc(cfg(all(feature = "async", not(any(feature = "lua51", feature = "luau")))))
373380
)]
374-
fn add_async_meta_method<M, A, MR, R>(&mut self, name: impl ToString, method: M)
381+
fn add_async_meta_method<M, A, MR, R>(&mut self, name: impl Into<StdString>, method: M)
375382
where
376383
T: 'static,
377384
M: Fn(Lua, UserDataRef<T>, A) -> MR + MaybeSend + 'static,
@@ -387,7 +394,7 @@ pub trait UserDataMethods<T> {
387394
/// [`add_meta_method_mut`]: UserDataMethods::add_meta_method_mut
388395
#[cfg(all(feature = "async", not(any(feature = "lua51", feature = "luau"))))]
389396
#[cfg_attr(docsrs, doc(cfg(feature = "async")))]
390-
fn add_async_meta_method_mut<M, A, MR, R>(&mut self, name: impl ToString, method: M)
397+
fn add_async_meta_method_mut<M, A, MR, R>(&mut self, name: impl Into<StdString>, method: M)
391398
where
392399
T: 'static,
393400
M: Fn(Lua, UserDataRefMut<T>, A) -> MR + MaybeSend + 'static,
@@ -400,7 +407,7 @@ pub trait UserDataMethods<T> {
400407
/// Metamethods for binary operators can be triggered if either the left or right argument to
401408
/// the binary operator has a metatable, so the first argument here is not necessarily a
402409
/// userdata of type `T`.
403-
fn add_meta_function<F, A, R>(&mut self, name: impl ToString, function: F)
410+
fn add_meta_function<F, A, R>(&mut self, name: impl Into<StdString>, function: F)
404411
where
405412
F: Fn(&Lua, A) -> Result<R> + MaybeSend + 'static,
406413
A: FromLuaMulti,
@@ -411,7 +418,7 @@ pub trait UserDataMethods<T> {
411418
/// This is a version of [`add_meta_function`] that accepts a `FnMut` argument.
412419
///
413420
/// [`add_meta_function`]: UserDataMethods::add_meta_function
414-
fn add_meta_function_mut<F, A, R>(&mut self, name: impl ToString, function: F)
421+
fn add_meta_function_mut<F, A, R>(&mut self, name: impl Into<StdString>, function: F)
415422
where
416423
F: FnMut(&Lua, A) -> Result<R> + MaybeSend + 'static,
417424
A: FromLuaMulti,
@@ -427,7 +434,7 @@ pub trait UserDataMethods<T> {
427434
docsrs,
428435
doc(cfg(all(feature = "async", not(any(feature = "lua51", feature = "luau")))))
429436
)]
430-
fn add_async_meta_function<F, A, FR, R>(&mut self, name: impl ToString, function: F)
437+
fn add_async_meta_function<F, A, FR, R>(&mut self, name: impl Into<StdString>, function: F)
431438
where
432439
F: Fn(Lua, A) -> FR + MaybeSend + 'static,
433440
A: FromLuaMulti,
@@ -446,7 +453,7 @@ pub trait UserDataFields<T> {
446453
///
447454
/// If `add_meta_method` is used to set the `__index` metamethod, it will
448455
/// be used as a fall-back if no regular field or method are found.
449-
fn add_field<V>(&mut self, name: impl ToString, value: V)
456+
fn add_field<V>(&mut self, name: impl Into<StdString>, value: V)
450457
where
451458
V: IntoLua + 'static;
452459

@@ -457,7 +464,7 @@ pub trait UserDataFields<T> {
457464
///
458465
/// If `add_meta_method` is used to set the `__index` metamethod, the `__index` metamethod will
459466
/// be used as a fall-back if no regular field or method are found.
460-
fn add_field_method_get<M, R>(&mut self, name: impl ToString, method: M)
467+
fn add_field_method_get<M, R>(&mut self, name: impl Into<StdString>, method: M)
461468
where
462469
M: Fn(&Lua, &T) -> Result<R> + MaybeSend + 'static,
463470
R: IntoLua;
@@ -470,21 +477,21 @@ pub trait UserDataFields<T> {
470477
///
471478
/// If `add_meta_method` is used to set the `__newindex` metamethod, the `__newindex` metamethod
472479
/// will be used as a fall-back if no regular field is found.
473-
fn add_field_method_set<M, A>(&mut self, name: impl ToString, method: M)
480+
fn add_field_method_set<M, A>(&mut self, name: impl Into<StdString>, method: M)
474481
where
475482
M: FnMut(&Lua, &mut T, A) -> Result<()> + MaybeSend + 'static,
476483
A: FromLua;
477484

478485
/// Add a regular field getter as a function which accepts a generic [`AnyUserData`] of type `T`
479486
/// argument.
480-
fn add_field_function_get<F, R>(&mut self, name: impl ToString, function: F)
487+
fn add_field_function_get<F, R>(&mut self, name: impl Into<StdString>, function: F)
481488
where
482489
F: Fn(&Lua, AnyUserData) -> Result<R> + MaybeSend + 'static,
483490
R: IntoLua;
484491

485492
/// Add a regular field setter as a function which accepts a generic [`AnyUserData`] of type `T`
486493
/// first argument.
487-
fn add_field_function_set<F, A>(&mut self, name: impl ToString, function: F)
494+
fn add_field_function_set<F, A>(&mut self, name: impl Into<StdString>, function: F)
488495
where
489496
F: FnMut(&Lua, AnyUserData, A) -> Result<()> + MaybeSend + 'static,
490497
A: FromLua;
@@ -497,7 +504,7 @@ pub trait UserDataFields<T> {
497504
///
498505
/// `mlua` will trigger an error on an attempt to define a protected metamethod,
499506
/// like `__gc` or `__metatable`.
500-
fn add_meta_field<V>(&mut self, name: impl ToString, value: V)
507+
fn add_meta_field<V>(&mut self, name: impl Into<StdString>, value: V)
501508
where
502509
V: IntoLua + 'static;
503510

@@ -509,7 +516,7 @@ pub trait UserDataFields<T> {
509516
///
510517
/// `mlua` will trigger an error on an attempt to define a protected metamethod,
511518
/// like `__gc` or `__metatable`.
512-
fn add_meta_field_with<F, R>(&mut self, name: impl ToString, f: F)
519+
fn add_meta_field_with<F, R>(&mut self, name: impl Into<StdString>, f: F)
513520
where
514521
F: FnOnce(&Lua) -> Result<R> + 'static,
515522
R: IntoLua;

0 commit comments

Comments
 (0)