Device

Struct Device 

Source
pub struct Device { /* private fields */ }
Expand description

An opened device connected via USB.

Implementations§

Source§

impl Device

Source

pub fn from_serial(serial: &str) -> Result<Option<Self>>

Searches the device by the serial number.

§Errors

Returns an error if the operation has failed.

§Examples
use libmtp::Device;

if let Some(device) = Device::from_serial("GVEV4I3E0WU1")? {
    println!("{device:?}");
} else {
    println!("Device not found");
}
Examples found in repository?
examples/copy-from-host.rs (line 6)
5fn main() -> libmtp::Result<()> {
6	let device = Device::from_serial("GVEV4I3E0WU1")?.expect("Device should exist");
7	let storage = device.find_storage(65537).expect("Storage should exist");
8	for object in storage.iter_recursive() {
9		if let Object::Folder(folder) = object {
10			folder.copy_file_from_host("/tmp/hello.txt", FileKind::Text)?;
11			break;
12		}
13	}
14	Ok(())
15}
More examples
Hide additional examples
examples/copy-to-host.rs (line 5)
4fn main() -> libmtp::Result<()> {
5	let device = Device::from_serial("GVEV4I3E0WU1")?.expect("Device should exist");
6	let storage = device.find_storage(65537).expect("Storage should exist");
7	for object in storage.iter_recursive() {
8		if let Object::File(file) = object {
9			let path = format!("/tmp/libmtp-{}", file.name());
10			file.copy_to_host(path)?;
11			break;
12		}
13	}
14	Ok(())
15}
Source

pub fn serial(&self) -> Result<String>

Retrieves the serial number of the device.

§Panics

Panics if the serial number of the device is not a valid UTF-8.

§Errors

Returns an error if the operation has failed.

§Examples
use libmtp::Device;

let device = Device::from_serial("GVEV4I3E0WU1")?.expect("Device should exist");
assert_eq!(device.serial()?, "GVEV4I3E0WU1");
Source

pub fn name(&self) -> Result<String>

Retieves the friendly name of the device.

§Errors

Returns an error if the device doesn’t have a support for friendly names, if the friendly name was not found or if the operation has failed.

§Examples
use libmtp::Device;

let device = Device::from_serial("GVEV4I3E0WU1")?.expect("Device should exist");
println!("{}", device.name()?);
Source

pub fn vendor(&self) -> Vendor<'_>

Retrieves the vendor of the device.

§Panics

Panics if the vendor name of the device is not a valid UTF-8.

§Examples
use libmtp::Device;

let device = Device::from_serial("GVEV4I3E0WU1")?.expect("Device should exist");
println!("{:?}", device.vendor());
Source

pub fn product(&self) -> Product<'_>

Retrieves the product of the device.

§Panics

Panics if the product name of the device is not a valid UTF-8.

§Examples
use libmtp::Device;

let device = Device::from_serial("GVEV4I3E0WU1")?.expect("Device should exist");
println!("{:?}", device.product());
Source

pub fn battery(&self) -> Result<Battery>

Retrieves the battery of the device.

§Errors

Returns an error if the operation has failed.

§Examples
use libmtp::Device;

let device = Device::from_serial("GVEV4I3E0WU1")?.expect("Device should exist");
println!("{:?}", device.battery()?);
Source

pub fn music_folder_id(&self) -> Option<u32>

Retrieves the ID of the default music folder of the device.

§Examples
use libmtp::Device;

let device = Device::from_serial("GVEV4I3E0WU1")?.expect("Device should exist");

if let Some(id) = device.music_folder_id() {
    println!("Music folder ID: {id}");
} else {
    println!("Music folder not found");
}
Source

pub fn playlist_folder_id(&self) -> Option<u32>

Retrieves the ID of the default playlists folder of the device.

§Examples
use libmtp::Device;

let device = Device::from_serial("GVEV4I3E0WU1")?.expect("Device should exist");

if let Some(id) = device.playlist_folder_id() {
    println!("Playlists folder ID: {id}");
} else {
    println!("Playlists folder not found");
}
Source

pub fn picture_folder_id(&self) -> Option<u32>

Retrieves the ID of the default pictures folder of the device.

§Examples
use libmtp::Device;

let device = Device::from_serial("GVEV4I3E0WU1")?.expect("Device should exist");

if let Some(id) = device.picture_folder_id() {
    println!("Pictures folder ID: {id}");
} else {
    println!("Pictures folder not found");
}
Source

pub fn video_folder_id(&self) -> Option<u32>

Retrieves the ID of the default videos folder of the device.

§Examples
use libmtp::Device;

let device = Device::from_serial("GVEV4I3E0WU1")?.expect("Device should exist");

if let Some(id) = device.video_folder_id() {
    println!("Videos folder ID: {id}");
} else {
    println!("Videos folder not found");
}
Source

pub fn organizer_folder_id(&self) -> Option<u32>

Retrieves the ID of the default organizers folder of the device.

§Examples
use libmtp::Device;

let device = Device::from_serial("GVEV4I3E0WU1")?.expect("Device should exist");

if let Some(id) = device.organizer_folder_id() {
    println!("Organizers folder ID: {id}");
} else {
    println!("Organizers folder not found");
}
Source

pub fn zencast_folder_id(&self) -> Option<u32>

Retrieves the ID of the default ZENcast folder of the device.

§Examples
use libmtp::Device;

let device = Device::from_serial("GVEV4I3E0WU1")?.expect("Device should exist");

if let Some(id) = device.zencast_folder_id() {
    println!("ZENcast folder ID: {id}");
} else {
    println!("ZENcast folder not found");
}
Source

pub fn album_folder_id(&self) -> Option<u32>

Retrieves the ID of the default albums folder of the device.

§Examples
use libmtp::Device;

let device = Device::from_serial("GVEV4I3E0WU1")?.expect("Device should exist");

if let Some(id) = device.album_folder_id() {
    println!("Albums folder ID: {id}");
} else {
    println!("Albums folder not found");
}
Source

pub fn text_folder_id(&self) -> Option<u32>

Retrieves the ID of the default texts folder of the device.

§Examples
use libmtp::Device;

let device = Device::from_serial("GVEV4I3E0WU1")?.expect("Device should exist");

if let Some(id) = device.text_folder_id() {
    println!("Texts folder ID: {id}");
} else {
    println!("Texts folder not found");
}
Source

pub fn refresh(&self) -> Result<()>

Refreshes storages information for the device.

§Errors

Returns an error if the operation has failed.

§Examples
use libmtp::Device;

let device = Device::from_serial("GVEV4I3E0WU1")?.expect("Device should exist");
let storage = device.find_storage(65537).expect("Storage should exist");

println!("Before: {}", storage.free_space());
device.refresh()?;
println!("After: {}", storage.free_space());
Source

pub fn rename(&self, name: &str) -> Result<()>

Changes the friendly name of the device.

§Errors

Returns an error if the device doesn’t have a support for friendly names or if the operation has failed.

§Panics

Panics if the friendly name of the storage contains a nul byte.

§Examples
use libmtp::Device;

let device = Device::from_serial("GVEV4I3E0WU1")?.expect("Device should exist");
device.rename("Bob's Phone")?;
Source

pub fn iter(&self) -> StorageIter<'_>

Retrieves an iterator over the storages of the device.

§Examples
use libmtp::Device;

let device = Device::from_serial("GVEV4I3E0WU1")?.expect("Device should exist");

for storage in &device {
    println!("{storage:?}");
}
Source

pub fn find_storage(&self, id: u32) -> Option<Storage<'_>>

Searches the storage of the device by the ID.

§Examples
use libmtp::Device;

let device = Device::from_serial("GVEV4I3E0WU1")?.expect("Device should exist");

if let Some(storage) = device.find_storage(65537) {
    println!("{storage:?}");
} else {
    println!("Storage not found");
}
Examples found in repository?
examples/copy-from-host.rs (line 7)
5fn main() -> libmtp::Result<()> {
6	let device = Device::from_serial("GVEV4I3E0WU1")?.expect("Device should exist");
7	let storage = device.find_storage(65537).expect("Storage should exist");
8	for object in storage.iter_recursive() {
9		if let Object::Folder(folder) = object {
10			folder.copy_file_from_host("/tmp/hello.txt", FileKind::Text)?;
11			break;
12		}
13	}
14	Ok(())
15}
More examples
Hide additional examples
examples/copy-to-host.rs (line 6)
4fn main() -> libmtp::Result<()> {
5	let device = Device::from_serial("GVEV4I3E0WU1")?.expect("Device should exist");
6	let storage = device.find_storage(65537).expect("Storage should exist");
7	for object in storage.iter_recursive() {
8		if let Object::File(file) = object {
9			let path = format!("/tmp/libmtp-{}", file.name());
10			file.copy_to_host(path)?;
11			break;
12		}
13	}
14	Ok(())
15}

Trait Implementations§

Source§

impl Debug for Device

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Hash for Device

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<'a> IntoIterator for &'a Device

Source§

type Item = Storage<'a>

The type of the elements being iterated over.
Source§

type IntoIter = StorageIter<'a>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more

Auto Trait Implementations§

§

impl Freeze for Device

§

impl RefUnwindSafe for Device

§

impl !Send for Device

§

impl !Sync for Device

§

impl Unpin for Device

§

impl UnwindSafe for Device

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.